data.frames的相等性,其中一列是列表

时间:2016-03-15 14:21:02

标签: r dataframe

以下是Hadley Wickham's Advanced R生成data.frame的示例,其中列有列表:

df1 <- data.frame(x=1:3)
df1$y <- list(1:2,1:3,1:4)

他接着解释说,也可以将data.frame创建为

df2 <- data.frame(x=1:3,y=I(list(1:2,1:3,1:4)))

两者都返回

  x          y
1 1       1, 2
2 2    1, 2, 3
3 3 1, 2, 3, 4

我的问题:我可以检查df1df2是否相同&#34;如果是,那么如何?

我尝试all.equal(df1,df2),这给了(抱歉在德国安装工作)

[1] "Component “y”: Attributes: < Ziel ist NULL, aktuell ist list >"
提供

identical(df1,df2)

[1] FALSE

以及all(df1==df2),返回

Error in FUN(left, right) : comparison of these types is not implemented

1 个答案:

答案 0 :(得分:1)

它们不相同,这是identical()正在检查的内容,它们有不同的类......

str(df1)
'data.frame':   3 obs. of  2 variables:
 $ x: int  1 2 3
 $ y:List of 3
  ..$ : int  1 2
  ..$ : int  1 2 3
  ..$ : int  1 2 3 4
str(df2)
'data.frame':   3 obs. of  2 variables:
 $ x: int  1 2 3
 $ y:List of 3
  ..$ : int  1 2
  ..$ : int  1 2 3
  ..$ : int  1 2 3 4
  ..- attr(*, "class")= chr "AsIs"

与此类似:

> a <- 1:3
> b <- 1:3
> class(b) <- "aaa"
> a
[1] 1 2 3
> b
[1] 1 2 3
attr(,"class")
[1] "aaa"
> identical(a,b)
[1] FALSE
> a==b
[1] TRUE TRUE TRUE