在R中如何比较两个数据框是否相同?

61

如何在R中检查两个对象(例如dataframes)是否相等?

所谓“值相等”,指的是一个dataframe中每行每列的数值与另一个dataframe相应位置的数值相等。


6
是使用?all.equal还是?identical?如果不是这两个函数,您需要详细说明您要比较的内容,以便我们了解您的问题。 - Justin
2
请看这里 - Tyler Rinker
1
“Value equal” 意思是什么? - David LeBauer
1
我投票关闭了,因为当前状态下它太含糊不清无法回答。 - IRTFM
5个回答

72

测试两个dataframe是否"值相等"并不清楚,但是为了测试值是否相同,这里有两个非完全相同但值相等的dataframe示例:

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

测试所有值是否相等:

all(a == b) # TRUE

测试对象是否相同(它们不相同,它们有不同的列名):

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

5
如果有人感到困惑,那是因为列名称不相同,所以它们并不完全相同。 - joran
1
@joran 感谢您指出这一点,我已经澄清了我的回答。 - David LeBauer
2
请注意,对于 identical 要返回 true,不仅必须匹配值和列名,还必须匹配行号/名称。(当我使用 subset() 时遇到此问题; 结果发现 all 才是我想要的。) - Darren Cook
@DavidLeBauer 有没有办法让“identical”忽略顺序? - Herman Toothrot
@user4050 是什么顺序?数值的顺序吗?你可以像这样对两个向量进行排序:identical(sort(a), sort(b)) - Abe

14

此外,相同仍然有用,并支持实际目标:

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

9
我们可以使用R包compare来测试对象的名称和值是否相同,只需一步即可。
a <- data.frame(x = 1:10)
b <- data.frame(y = 1:10)

library(compare)
compare(a, b)
#FALSE [TRUE]#objects are not identical (different names), but values are the same.

如果我们只关心值的相等性,我们可以设置ignoreNames=TRUE

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

该包还具有其他有趣的函数,如compareEqualcompareIdentical

1
这里有另一种方法,使用arsenal包中的comparedf函数。
它会按变量给出差异,未共享的变量(例如不同列),未共享的观测数以及总体比较的摘要。
df1 <- data.frame(id = paste0("person", 1:3),
                  a = c("a", "b", "c"),
                  b = c(1, 3, 4))

> df1
         id     a       b 
1     person1   a       1 
2     person2   b       3
3     person3   c       4


df2 <- data.frame(id = paste0("person", 4:1),
                  a = c("c", "b", "a", "f"),
                  b = c(1, 3, 4, 4),
                  d = paste0("rn", 1:4))

> df2

        id     a     b     d

1     person4  c     1    rn1
2     person3  b     3    rn2
3     person2  a     4    rn3
4     person1  f     4    rn4


library(arsenal)
comparedf(df1, df2)

Compare Object
Function Call: 
comparedf(x = df1, y = df2)

Shared: 3 non-by variables and 3 observations.
Not shared: 1 variables and 0 observations.

Differences found in 2/3 variables compared.
0 variables compared have non-identical attributes.

有可能获取更详细的摘要

 summary(comparedf(df1, df2))

下面的代码将返回几个表格:
  • 数据框的摘要
  • 整体比较的摘要
  • 不共享的变量
  • 其他未比较的变量
  • 未共享的观测值
  • 按变量检测到的差异
  • 检测到的差异
  • 非相同属性
在这里您可以查看有关该包和函数的更多信息
此外,您还可以使用all.equal(df1, df2)
[1] "Attributes: < Component “row.names”: Numeric: lengths (3, 4) differ >"
[2] "Length mismatch: comparison on first 3 components"                    
[3] "Component “id”: Lengths (3, 4) differ (string compare on first 3)"    
[4] "Component “id”: 3 string mismatches"                                  
[5] "Component “a”: Lengths (3, 4) differ (string compare on first 3)"     
[6] "Component “a”: 2 string mismatches"                                   
[7] "Component “b”: Numeric: lengths (3, 4) differ"

0
不需要依赖其他包,比较两个数据集的结构(类和属性):
structure_df1 <- sapply(df1, function(x) paste(class(x), attributes(x), collapse = ""))
structure_df2 <- sapply(df2, function(x) paste(class(x), attributes(x), collapse = ""))

all(structure_df1 == structure_df2)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接