使用rbindlist()将循环结果追加到data.table

使用rbindlist()将循环结果追加到data.table,r,data.table,R,Data.table,我正在使用trubles将来自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'.

我正在使用trubles将来自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)

为什么我会犯这个错误

尝试使用gtools软件包中的smartbind

library(gtools)

dat = do.call(smartbind, results)
希望这能解决您的问题

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