使用rbind合并数据表时,其中一个为空会出现错误

6

当使用rbind合并三个或者更多的data.table时,如果在两个非空的data.table之间存在一个空的data.table,则会出现错误:

> require(data.table)
Loading required package: data.table
data.table 1.9.2  For help type: help("data.table")

> DT <- data.table(x=c(1,2,3))
> rbind(DT, data.table(), DT)
Error in setcolorder(tt, chmatch(match.names, make.names(original.names[[x]],  (from <text>#1) : 
  Column numbers in neworder out of bounds: NA

如果空的data.table在末尾,则不会发生这种情况:

> rbind(DT, DT, data.table()) 
   x
1: 1
2: 2
3: 3
4: 1
5: 2
6: 3

但是,当空的 data.table 位于开头时,会发生其他错误:

> rbind(data.table(), DT, DT) 
Error in data.table::.rbind.data.table(...) (from <text>#1) : 
  Some colnames of argument 2 (x) are not present in colnames of item 1. If an argument has colnames they can be in a different order, but they must all be present. Alternatively, you can supply unnamed lists and they will then be joined by position. Or, set use.names=FALSE.

然而,如果我们只使用两个参数,一切都很好:

> rbind(data.table(), DT) 
   x
1: 1
2: 2
3: 3

期望的行为是什么?
2个回答

4
这似乎是由于data.table::.rbind.data.table中的不一致行为引起的。添加以下行可以修复此问题。请向data.table开发人员提交错误报告。
....
original.names = lapply(list(...), names)
allargs = lapply(list(...), as.data.table)
original.names = original.names[sapply(allargs, length) > 0L]  # this is the new line
allargs = allargs[sapply(allargs, length) > 0L]
...

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接