Test Equality of Two Objects Using identical Function in R

Identical function in R

Identical function in R is one of the best way to test whether two objects are exactly equal. R Identical function, returns TRUE when two objects are equal and it returns FALSE when two objects are not equal.

Syntax for identical function in R

Identical(x, y, num.eq = TRUE)
x,y R Objects
num.eq Default value is TRUE, when FALSE identifies the difference between -0 and 0

 

Example of identical function in R:


identical(2,2)
[1] TRUE

 

 

# R identical function with arithmetic operation

identical(2,8/4)
[1] TRUE

 

 

# Identical function comparing numeric and character object

identical("2",2)
[1] FALSE

 

 

# Identical function comparing Infinity and –Infinity

identical(Inf,-Inf)
[1] FALSE

 

 

# Identical function comparing 0 and -0

identical(0,-0)
[1] TRUE

Logically the above example sounds TRUE, but still if you want to differentiate between -0 and 0 you can use num.eq=FALSE argument in identical function

 

# R Identical function with num.eq=FALSE

identical(0,-0, num.eq=FALSE)

So the output will be

[1] FALSE

 

Example of identical function in R for vectors:

# R Identical function for vectors

a = c(1,2,4,5)
b = c(1,5,4,2)
identical(a,b)
[1] FALSE

 

 


a = c(1,2,4,5)
b = c(1,2,4,5)
identical(a,b)
[1] TRUE

previous small identical function in r                                                                                                           next small identical function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts