The identical() function performs a strict equality test;it checks if two objects are exactly the same in every way. Unlike == which compares values, identical() checks structure, attributes, and exact representation. This is crucial for testing and debugging when you need to be absolutely certain two objects are the same.
When to Use identical()
You’ll use identical() when:
- Testing exact object equality in unit tests
- Detecting if objects are truly the same (not just equal values)
- Checking for changes after operations
- Validating function outputs
- Comparing object structure and attributes
Key Difference: identical() vs ==
# identical() checks EXACT representation
identical(1, 1.0) # FALSE (different types: integer vs numeric)
identical(c(1), c(1.0)) # FALSE
# == checks VALUE equality
1 == 1.0 # TRUE
c(1) == c(1.0) # TRUE
Basic Syntax
identical(x, y, num.eq = TRUE)
- x, y: Objects to compare
- num.eq: If TRUE, treats close numeric values as equal
Examples with Explanations
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 returns FALSE means values in both data frames are not equal.
Common Mistakes to Avoid
Mistake 1: Confusing identical() with ==
# ❌ WRONG - Using == for structure checking
x <- c(1L, 2L, 3L) # integer vector
y <- c(1, 2, 3) # numeric vector
x == y # [1] TRUE TRUE TRUE (only checks values)
# ✅ CORRECT - Use identical() for exact equality
identical(x, y) # FALSE (different types!)
identical(x, as.numeric(x)) # FALSE (still different)
Mistake 2: Names and attributes matter
# ❌ WRONG - Ignoring names/attributes
v1 <- c(a=1, b=2, c=3)
v2 <- c(1, 2, 3)
identical(v1, v2) # FALSE (different names!)
# ✅ CORRECT - Names must match
v3 <- c(a=1, b=2, c=3)
identical(v1, v3) # TRUE
Mistake 3: Floating point precision issues
# ❌ PROBLEM - Floating point rounding errors
x <- 0.1 + 0.2
y <- 0.3
identical(x, y) # FALSE!
x == y # FALSE (shows numeric precision issue)
# ✅ SOLUTION - Use all.equal() for approximate equality
all.equal(x, y) # TRUE (allows for rounding errors)
Mistake 4: Factor vs character confusion
# ❌ WRONG - Assuming factors and characters are the same
f <- factor(c("a", "b", "c"))
c <- c("a", "b", "c")
identical(f, c) # FALSE (different class)
# ✅ CORRECT - Check classes first
class(f) # "factor"
class(c) # "character"
Pro Tips
- Use identical() for testing: Perfect for unit tests
stopifnot(identical(result, expected_value)) - Check object structure: Shows you exact differences
if (!identical(x, y)) { str(x) str(y) } - Use all.equal() for approximate floating point comparisons
- Attributes matter: identical() catches attribute differences