我刚刚发现了这个错误,才发现有些人称它为“功能”。这使得rbindlist
NOT like WILL 尊重列名do.call("rbind",l)
。rbind
此外,文档中没有提到这种完全出乎意料的行为。这真的是故意的吗?
代码示例:
> 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
这些会产生 a = 1,4 的列;b = 2,3。rbind.data.table
用and得到它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