简体   繁体   English

使用rbind合并data.tables时出错,其中之一为空

[英]Error when using rbind to merge data.tables and one of them is empty

An error is issued when merging three (or more) data.tables with rbind and there is an empty data.table between two non-empty data.tables: 将三个(或多个)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

This does not happen if the empty data.table is in the end: 如果最后一个空的data.table不会发生这种情况:

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

But some other error happens when the empty data.table is in the beginning: 但是,当空的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.

However, if we use only two arguments everything is fine: 但是,如果仅使用两个参数,则一切都很好:

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

What should be the expected behavior? 预期的行为应该是什么?

This seems to be due to inconsistent behavior in data.table::.rbind.data.table . 这似乎是由于data.table::.rbind.data.table行为不一致data.table::.rbind.data.table Adding the following line would fix this. 添加以下行将解决此问题。 Please file a bug-report with the data.table developers. 请向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]
...

Bug #5612 is now fixed in commit #1266 (v1.9.3). 错误#5612现在已在提交#1266(v1.9.3)中修复。 From NEWS : 来自新闻

o Implementing #5249 closes bug #5612 , a case where rbind gave error when binding with empty data.tables as shown here: Error when using rbind to merge data.tables and one of them is empty . o实施#5249关闭了错误#5612 ,在这种情况下rbind与空的data.tables绑定时出现错误,如下所示: 使用rbind合并data.tables时出错,其中一个为empty Thanks to Roger for reporting on SO. 感谢Roger报道了SO。

Now, the line: 现在,该行:

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

gives: 给出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM