【问题标题】:appending results from a loop to a data.table with rbindlist()使用 rbindlist() 将循环中的结果附加到 data.table
【发布时间】:2020-05-20 06:24:21
【问题描述】:

我在将 for 循环中的结果附加到之前创建的 data.table 时遇到问题。我正在使用rbnindlist 并得到一个错误:

Error in rbindlist(policyBacktest2, backtestProducts2, use.names = TRUE,  : 
  idcol must be a logical or character vector of length 1. If logical TRUE the id column will named '.id'.

我使用循环来获取一些数据。

  for (policy in policies){
    # procedures
    }

使用rbindlist 我已经转换了你 将占位符表以及结果表放入列表中:

> results
[[1]]
[[1]][[1]]
    fromDate1     plan1      region1 noins1  nextplan noinsnext nextvalue   regionnext  fromDate2     plan2 noins2      region2


> placeholder
[[1]]
[[1]][[1]]
   policyID nextplan noinsnext nextvalue regionnext fromdate1 fromdate2 plan1 plan2 noins1 noins2 region1 region2 value1month12

然后我用了:

result <- rbindlist(results, placeholder, use.names = TRUE, fill = TRUE)

为什么会出现这个错误?

【问题讨论】:

  • 我不认为rbindlist 可以按照您希望的方式工作。我认为您需要将两个表放在一个列表中并使用rbindlist 绑定这两个表。不是两个列表并试图将它们绑定在一起

标签: r data.table


【解决方案1】:

尝试使用 gtools 包中的 smartbind

library(gtools)

dat = do.call(smartbind, results)

希望这能解决您的问题

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2012-03-23
    • 2015-07-26
    • 2019-04-09
    • 2021-08-28
    • 2013-07-24
    相关资源
    最近更新 更多