The identical() function is used compare two object and check whether they are identical or not.This function gives output in boolean value means it gives TRUE when two objects are identical and it gives FALSE when two objects are not identical.

The following method show how you can do it with syntax.

Method: Use identical() Function

identical(object1,object2)

The following example shows how to use identical() function in R.

Use identical() to Check if Two Strings are Identical or Not

Let’s see how we can comapre two string and check they are equal or not:

# Define string
string1 <- "This is R language"

string2 <- "This is R language"

# Check strings are identical or not
identical(string1,string2)

Output:

[1] TRUE

The output shows the strings are identical.

This function gives output as TRUE means both strings are identical.

Let’s see another example to check strings are identical or not:

# Define string
string1 <- "This is R Language"

string2 <- "This is R language"

# Check strings are identical or not
identical(string1,string2)

Output:

[1] FALSE

As the output shows strings are not same.

Use identical() to Check if Two Vectors are Identical or Not

Suppose you have two vectors and want to check they both are equal or not:

# Define vectors
temp1 <- c(12, 20, 25, 19)

temp2 <- c(12, 22, 25, 32)

# Check vectors are identical or not
identical(temp1,temp2)

Output:

[1] FALSE

As in the output we can see function returns FALSE means values in vectors are different.

Use identical() to Check Data Frames are Equal or Not

Use identical() function to check whether two data frame are equal or not:

# Define data frame
df1 <- data.frame(Machine_name=c("B","C","E","F","G","I"),
                  Temp=c(81,89,77,84,75,74))

df2 <- data.frame(Machine_name=c("B","C","E","F","G","I"),
                  Temp=c(81,89,77,84,75,74))

# Check data frames are identical or not
identical(df1,df2)

Output:

[1] TRUE

The identical() function return TRUE means values in both data frame are same.

# Define data frame
df1 <- data.frame(Machine_name=c("A","C","D","F","B","H"),
                  Temp=c(77,89,78,84,81,73))

df2 <- data.frame(Machine_name=c("B","C","E","F","G","I"),
                  Temp=c(81,89,77,84,75,74))

# Check data frames are identical or not
identical(df1,df2)

Output:

[1] FALSE

The identical() function return FALSE means values in both data frame are not equal.