为什么rbindlist不尊重列名?
我刚刚发现了这个bug,但却发现有些人把它称为一个错误。这使得为什么rbindlist不尊重列名?,r,data.table,rbind,R,Data.table,Rbind,我刚刚发现了这个bug,但却发现有些人把它称为一个错误。这使得rbindlist不像do。调用(“rbind”,l)asrbind将尊重列名。此外,文档中没有提到这种完全出乎意料的行为。这真的是故意的吗 代码示例: > library(data.table) > DT1 <- data.table(a=1, b=2) > DT2 <- data.table(b=3, a=4) > DT1 a b 1: 1 2 > DT2 b a 1: 3 4 但是,r
rbindlist
不像do。调用(“rbind”,l)
asrbind
将尊重列名。此外,文档中没有提到这种完全出乎意料的行为。这真的是故意的吗
代码示例:
> library(data.table)
> DT1 <- data.table(a=1, b=2)
> DT2 <- data.table(b=3, a=4)
> DT1
a b
1: 1 2
> DT2
b a
1: 3 4
但是,rbindlist
很乐意以静默方式损坏数据:
> rbindlist(list(DT1, DT2))
a b
1: 1 2
2: 3 4
此功能现在在中实现。发件人:
使用此选项,您可以将use.names=TRUE
设置为按名称绑定。默认情况下,它设置为FALSE
,以实现向后兼容性。或者,您可以使用rbind(..)
其中use.names=TRUE
,同样是为了向后兼容
有关更多示例和基准,请参见
示例:
1) 只需设置use.names=TRUE
DT1 <- data.table(x=1, y=2)
DT2 <- data.table(y=1, x=2)
rbindlist(list(DT1,DT2), use.names=TRUE, fill=FALSE)
# x y
# 1: 1 2
# 2: 2 1
DT1 <- data.table(x=1, y=2)
DT2 <- data.table(z=2, y=1)
# returns error when fill=FALSE but can't be bound without fill=TRUE
rbindlist(list(DT1, DT2), use.names=TRUE, fill=FALSE)
# Error in rbindlist(list(DT1, DT2), use.names = TRUE, fill = FALSE) :
# Answer requires 3 columns whereas one or more item(s) in the input
# list has only 2 columns. ...
HTH看看这个。
rbindlist
针对速度进行了优化。匹配列名会适得其反,我希望默认行为不会改变。但是,我可以自由地提交一个功能请求。斯文,我在我的帖子中链接到了这个。这对我来说似乎不是特别权威。罗兰,如果你在破坏数据,速度是没有用的。默默地听着。此外,如果名称不受尊重,那么使用带有命名列的数据结构有什么意义呢?@Roland,我也喜欢这个功能。也许一个参数match.names=TRUE
很好?@James,是的,文档中似乎遗漏了这个参数。将编辑。谢谢
o 'rbindlist' gains 'use.names' and 'fill' arguments and is now implemented
entirely in C. Closes #5249
-> use.names by default is FALSE for backwards compatibility (doesn't bind by
names by default)
-> rbind(...) now just calls rbindlist() internally, except that 'use.names'
is TRUE by default, for compatibility with base (and backwards compatibility).
-> fill by default is FALSE. If fill is TRUE, use.names has to be TRUE.
-> At least one item of the input list has to have non-null column names.
-> Duplicate columns are bound in the order of occurrence, like base.
-> Attributes that might exist in individual items would be lost in the bound result.
-> Columns are coerced to the highest SEXPTYPE, if they are different, if/when possible.
-> And incredibly fast ;).
-> Documentation updated in much detail. Closes DR #5158.
DT1 <- data.table(x=1, y=2)
DT2 <- data.table(y=1, x=2)
rbindlist(list(DT1,DT2), use.names=TRUE, fill=FALSE)
# x y
# 1: 1 2
# 2: 2 1
DT1 <- data.table(x=1, y=2)
DT2 <- data.table(z=2, y=1)
# returns error when fill=FALSE but can't be bound without fill=TRUE
rbindlist(list(DT1, DT2), use.names=TRUE, fill=FALSE)
# Error in rbindlist(list(DT1, DT2), use.names = TRUE, fill = FALSE) :
# Answer requires 3 columns whereas one or more item(s) in the input
# list has only 2 columns. ...
DT1 <- data.table(x=1, x=2, y=10, y=20, y=30)
DT2 <- data.table(y=-10, x=-2, y=-20, x=-1, y=-30)
rbindlist(list(DT1,DT2), use.names=TRUE)
# x x y y y
# 1: 1 2 10 20 30
# 2: -2 -1 -10 -20 -30
DT1 <- data.table(x=1, y=2)
DT2 <- data.table(y=2, z=-1)
rbindlist(list(DT1, DT2), fill=TRUE)
# x y z
# 1: 1 2 NA
# 2: NA 2 -1