Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare if two dataframe objects in R are equal?

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.

like image 206
mindless.panda Avatar asked May 14 '12 22:05

mindless.panda


People also ask

How do I compare two sets of data in R?

We can use the compare package in R. We can easily use this package to compare two data frames and check out the summary of what extent it is changed. The function comparedf() is used to compare two dataframes in R. The function takes two dataframes and then check them for comparison.

How do I check if two columns are the same in R?

We can compare two columns in R by using ifelse(). This statement is used to check the condition given and return the data accordingly.


2 Answers

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. 
like image 154
David LeBauer Avatar answered Sep 25 '22 22:09

David LeBauer


In addition, identical is still useful and supports the practical goal:

identical(a[, "x"], b[, "y"]) # TRUE 
like image 20
Brad Horn Avatar answered Sep 25 '22 22:09

Brad Horn