Compare if two dataframe objects in R are equal?

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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.

回答1:

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 

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. 


回答2:

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

identical(a[, "x"], b[, "y"]) # TRUE 


回答3:

We can use the R package compare to test whether the names of the object and the values are the same, in just one step.

a 

In case we only care about equality of the values, we can set ignoreNames=TRUE

compare(a, b, ignoreNames=T) #TRUE #  dropped names 

The package has additional interesting functions such as compareEqual and compareIdentical.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!