Home » How to Use the identical() Function in R (With Examples)

How to Use the identical() Function in R (With Examples)

by Erma Khan

The identical() function in R can be used to test whether or not two objects in R are exactly equal.

This function uses the following basic syntax:

identical(x, y, …)

where:

  • x: The name of an object in R
  • y: The name of another object in R

This function returns TRUE if the two objects are exactly equal or FALSE if they are not.

The following examples show how to use this function to test if two strings, two vectors, and two data frames are exactly equal.

Example 1: Use identical() to Test if Two Strings are Equal

The following code shows how to use the identical() function to test if two strings are equal:

#define two strings
string1 #check if two strings are identical
identical(string1, string2)

[1] TRUE

The function returns TRUE since the two strings are indeed exactly identical.

The following code shows how to use the identical() function to test if another two strings are exactly equal:

#define two strings
string1 #check if two strings are identical
identical(string1, string2)

[1] FALSE

The function returns FALSE since the two strings are not exactly identical.

Example 2: Use identical() to Test if Two Vectors are Equal

The following code shows how to use the identical() function to test if two vectors are equal:

#define two vectors
vector1 #check if two vectors are identical
identical(vector1, vector2)

[1] TRUE

The function returns TRUE since the two vectors are indeed exactly identical.

The following code shows how to use the identical() function to test if another two vectors are exactly equal:

#define two vectors
vector1 #check if two vectors are identical
identical(vector1, vector2)

[1] FALSE

The function returns FALSE since the two vectors are not exactly identical.

Example 3: Use identical() to Test if Two Data Frames are Equal

The following code shows how to use the identical() function to test if two data frames are equal:

#define two data frames
df1 frame(team=c('A', 'B', 'C', 'D'),
                  points=c(14, 20, 22, 29))

df2 frame(team=c('A', 'B', 'C', 'D'),
                  points=c(14, 20, 22, 29))

#check if two data frames are equal
identical(df1, df2)

[1] TRUE

The function returns TRUE since the two data frames are indeed exactly identical.

The following code shows how to use the identical() function to test if another two data frames are exactly equal:

#define two data frames
df1 frame(team=c('A', 'B', 'C', 'D'),
                  points=c(14, 20, 22, 29))

df2 frame(team=c('A', 'B', 'C', 'D'),
                  points=c(99, 20, 22, 29))

#check if two data frames are equal
identical(df1, df2)

[1] FALSE

The function returns FALSE since the two data frames are not exactly identical.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use the dim() Function in R
How to Use the transform() Function in R
How to Use the intersect() Function in R

Related Posts