注意:新手。我有几个data.tables,每个都有数百万行,变量主要是日期和因素。我正在使用rbindlist()将它们because组合在一起。昨天,在将表垂直拆分成较小的块之后(而不是当前的水平拼接),我试图更好地理解rbind(尤其是fill = TRUE),还尝试了bind_rows(),然后尝试验证结果但相同( )返回FALSE。
library(data.table)
library(dplyr)
DT1 <- data.table(a=1, b=2)
DT2 <- data.table(a=4, b=3)
DT_bindrows <- bind_rows(DT1,DT2)
DT_rbind <- rbind(DT1,DT2)
identical(DT_bindrows,DT_rbind)
# [1] FALSE
目视检查bind_rows()和rbind()的结果,它们确实是相同的。我读了this和this(从我改编示例的地方)。我的问题是:(a)我缺少什么,(b)如果列的数目,名称和顺序相同,我是否应该担心same()= FALSE?
答案 0 :(得分:5)
identical
检查attributes
是否不同。使用all.equal
,可以选择不检查属性(check.attributes
)
all.equal(DT_bindrows, DT_rbind, check.attributes = FALSE)
#[1] TRUE
如果我们同时检查两个数据集的str
,它将变得很清晰
str(DT_bindrows)
#Classes ‘data.table’ and 'data.frame': 2 obs. of 2 #variables:
# $ a: num 1 4
# $ b: num 2 3
str(DT_rbind)
#Classes ‘data.table’ and 'data.frame': 2 obs. of 2 #variables:
# $ a: num 1 4
# $ b: num 2 3
# - attr(*, ".internal.selfref")=<externalptr> # reference attribute
通过将属性分配为NULL,identical
返回TRUE
attr(DT_rbind, ".internal.selfref") <- NULL
identical(DT_bindrows, DT_rbind)
#[1] TRUE