比较R中的两个对象是否相等?
问题描述:
如何检查两个物件,例如数据帧,在R中的值相等
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.
答
不清楚测试两个数据帧是否为值相等以测试值是否相同,这里是具有相等值的两个不相同的数据帧的示例:
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
b
$ b
To test if objects are identical (they are not, they have different column names):
identical(a,b) # FALSE: class, colnames, rownames must all match.