Compare if two dataframe objects in R are equal?

mindless.panda picture mindless.panda · May 15, 2012 · Viewed 76.3k times · Source

How do I check if two objects, e.g. dataframes, are value equal in R?

By value equal, I mean the value of each row of each column of one dataframe is equal to the value of the corresponding row and column in the second dataframe.

Answer

David LeBauer picture David LeBauer · May 15, 2012

It is not clear what it means to test if two data frames are "value equal" but to test if the values are the same, here is an example of two non-identical dataframes with equal values:

a <- data.frame(x = 1:10)
b <- data.frame(y = 1:10)

To test if all values are equal:

all(a == b) # TRUE

To test if objects are identical (they are not, they have different column names):

identical(a,b) # FALSE: class, colnames, rownames must all match.