2016-03-15 69 views
1

下面是一个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是否“相同”,如果是这样,怎么办?

我试过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 
+2

什么'all.equal(DF1,DF2,check.attributes = FALSE)'? – nrussell

+0

是的,非常感谢! –

+0

你愿意教育我为什么这是题外话?我承认不了解“具体原因”。 –

回答

1

他们是不一样的,这是wha牛逼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