4
我刚刚发现了这个bug,却发现有人叫它"feature"。这使rbindlist
不像do.call("rbind",l)
将尊重列名称。此外,在文档中没有提到这种完全意外的行为。这真的是故意的吗?为什么rbindlist不尊重列名?
代码例如:
> 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
我期望rbind
“荷兰国际集团这些会产生具有= 1,4的列; b = 2,3。并得到rbind.data.table
和rbind.data.frame
,虽然rbind.data.table
产生警告。
> rbind(DT1, DT2)
a b
1: 1 2
2: 4 3
Warning message:
In data.table::.rbind.data.table(...) :
Argument 2 has names in a different order. Columns will be bound by name for consistency with base. You can drop names (by using an unnamed list) and the columns will then be joined by position, or set use.names=FALSE. Alternatively, explicitly setting use.names to TRUE will remove this warning.
> rbind(as.data.frame(DT1), as.data.frame(DT2))
a b
1 1 2
2 4 3
> do.call('rbind', list(DT1, DT2))
a b
1: 1 2
2: 4 3
Warning message:
In data.table::.rbind.data.table(...) :
Argument 2 has names in a different order. Columns will be bound by name for consistency with base. You can drop names (by using an unnamed list) and the columns will then be joined by position, or set use.names=FALSE. Alternatively, explicitly setting use.names to TRUE will remove this warning.
rbindlist
,但是,很高兴地默默破坏数据:
> rbindlist(list(DT1, DT2))
a b
1: 1 2
2: 3 4
看一看这个[出色答卷(http://stackoverflow.com/a/15673654/1627235)。 –
'rbindlist'针对速度进行了优化。匹配列名称会适得其反,我希望默认行为不会改变。但是,可以免费提交功能请求。 – Roland
斯文,我链接到我的文章。这对我来说似乎并不特别权威。罗兰,如果你正在破坏数据,速度毫无用处。默默地在那。此外,如果名称不被尊重,那么使用具有命名列的数据结构有什么意义? – James