What is the identical() function in R?

Overview

The identical() function in R is used to test if two given R objects are exactly equal.

Syntax

identical(a, b)

Parameters

The identical() function takes the parameter values a and b that represent the R objects to be compared.

Note: The identical() function can take two or more objects.

Return value

The identical() function returns either a TRUE or FALSE.

Example

# creating different R objects to be compared
a <- c('Bar', 'Baz', 'Foo' );
b <- c('Baz', 'Foo', 'Bar' );
c <- c('Bar', 'Baz', 'Foo' );
d <- c('Bar', 'Baz', 'Foo', 'Foo', 'Foo');
# checking if a and b are equal
identical(a, b)
# checking if a and c are equal
identical(a, c)
# checking if a, and d are equal
identical(a, d)

Explanation

  • Lines 2–5: We create R objects a, b, c and d for comparison.
  • Line 8: We check if the objects a and b are equal using the identical() function.
  • Line 11: We check if the objects a and c are equal using the identical() function.
  • Line 14: We check if the objects a and d are equal using the identical() function.

Free Resources