使用rbind合并data.tables时出错,其中一个是空的

时间:2014-04-22 09:49:27

标签: r data.table rbind

将三个(或更多)data.tables与rbind合并时发出错误,并且两个非空data.tables之间存在空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 个答案:

答案 0 :(得分: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 :(得分:1)

Bug #5612现在已在提交#1266(v1.9.3)中修复。来自NEWS

  

o实现#5249会关闭错误#5612,这是rbind在绑定空data.tables时出错的情况,如下所示:Error when using rbind to merge data.tables and one of them is empty。感谢Roger报告SO。

现在,行:

> rbind(data.table(), DT, DT) 

给出:

#    x
# 1: 1
# 2: 2
# 3: 3
# 4: 1
# 5: 2
# 6: 3