有效地将列表转换为data.table

时间:2018-04-11 19:01:42

标签: r data.table

我有一个看起来像这样的列表

    ans<-    list(structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=1,total_lmp_rt=65.141702,congestion_price_rt=-0.94675,
marginal_loss_price_rt=0.048453),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=3,total_lmp_rt=57.007182,congestion_price_rt=-9.771097,
marginal_loss_price_rt=0.738279),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48592,total_lmp_rt=48.61,congestion_price_rt=-17.31,
marginal_loss_price_rt=-0.12),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48593,total_lmp_rt=48.61,congestion_price_rt=-17.31,
marginal_loss_price_rt=-0.12),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48594,total_lmp_rt=47.35,congestion_price_rt=-18.7,
marginal_loss_price_rt=0.01),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48595,total_lmp_rt=47.35,congestion_price_rt=-18.7,
marginal_loss_price_rt=0.01),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48596,total_lmp_rt=47.35,congestion_price_rt=-18.7,
marginal_loss_price_rt=0.01),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48603,total_lmp_rt=47.86,congestion_price_rt=-18.21,
marginal_loss_price_rt=0.03),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48604,total_lmp_rt=47.86,congestion_price_rt=-18.21,
marginal_loss_price_rt=0.03),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)),structure(list(datetime_beginning_ept="2018-03-21T00:00:00",
pnode_id=48605,total_lmp_rt=50.3,congestion_price_rt=-15.4,
marginal_loss_price_rt=-0.34),.Names=c("datetime_beginning_ept",
"pnode_id","total_lmp_rt","congestion_price_rt","marginal_loss_price_rt"
)))

我想让它成为一个看起来像这样的data.table ......

pricedate              node_id         dalmp      damcc     damcl
1: 2018-03-21T00:00:00       1  100 65.14170  -0.946750  0.048453
2: 2018-03-21T00:00:00       3  100 57.00718  -9.771097  0.738279
3: 2018-03-21T00:00:00   48592  100 48.61000 -17.310000 -0.120000
4: 2018-03-21T00:00:00   48593  100 48.61000 -17.310000 -0.120000
5: 2018-03-21T00:00:00   48594  100 47.35000 -18.700000  0.010000
6: 2018-03-21T00:00:00   48595  100 47.35000 -18.700000  0.010000
7: 2018-03-21T00:00:00   48596  100 47.35000 -18.700000  0.010000
8: 2018-03-21T00:00:00   48603  100 47.86000 -18.210000  0.030000
9: 2018-03-21T00:00:00   48604  100 47.86000 -18.210000  0.030000
10: 2018-03-21T00:00:00   48605  100 50.30000 -15.400000 -0.340000

现在我正在这样做

rbindlist(lapply(1:length(ans), function(x) {
  return(data.table(pricedate=ans[[x]]$datetime_beginning_ept, node_id=ans[[x]]$pnode_id, dalmp=ans[[x]]$total_lmp_rt,  damcc=ans[[x]]$congestion_price_rt, damcl=ans[[x]]$marginal_loss_price_rt))
}))

它的工作正常,但在现实生活中我的ans长达50,000行,我必须迭代它们的几个案例,并且每次需要几分钟。有没有更好的方法来进行这种转换(除了并行化任务)?

0 个答案:

没有答案