do.call 和 rbind for list with order 作为新的列名

do.call and rbind for list with order as new column name

我在重现这个例子时遇到问题,但我有一个由 253 个对象组成的列表,每个对象有 4 列,长度为 3000:

> str(data)
List of 253
 $ : num [1:3000, 1:4] 0.269 0.201 0.155 0.329 0.307 ...
 $ : num [1:3000, 1:4] 0.269 0.203 0.156 0.329 0.307 ...
 $ : num [1:3000, 1:4] 0.269 0.204 0.156 0.329 0.307 ...
 $ : num [1:3000, 1:4] 0.27 0.205 0.156 0.329 0.307 ...

我正在尝试使用 do.call 和 rbind 创建一个包含四列的新对象和另一列的列表编号,如下所示:

head(new)
     Name             [,1]               [,2]              [,3]              [,4]
[1,] 1    0.269077013009290 0.2522168552880047 0.262594373389746 0.216111758312960
[2,] 1    0.201348056547152 0.2611904119569095 0.264782271654359 0.272679259841580
[3,] 1    0.155481831431131 0.2599908556717349 0.412742657066444 0.171784655830690
[4,] 1    0.329421949966686 0.0968555482105167 0.365492803829526 0.208229697993272
[5,] 1    0.306985894860771 0.1407298652926539 0.331197510871706 0.221086728974869
[6,] 1    0.147101456518243 0.1521556602567531 0.402434842158681 0.298308041066323

当前使用以下代码给出:

 new <- do.call(rbind, data)
 head (new)
             [,1]               [,2]              [,3]              [,4]
[1,] 0.269077013009290 0.2522168552880047 0.262594373389746 0.216111758312960
[2,] 0.201348056547152 0.2611904119569095 0.264782271654359 0.272679259841580
[3,] 0.155481831431131 0.2599908556717349 0.412742657066444 0.171784655830690
[4,] 0.329421949966686 0.0968555482105167 0.365492803829526 0.208229697993272
[5,] 0.306985894860771 0.1407298652926539 0.331197510871706 0.221086728974869
[6,] 0.147101456518243 0.1521556602567531 0.402434842158681 0.298308041066323

任何关于如何添加 "Name" 列的想法都将不胜感激!

我们可以用 Map 创建一个 'Name' 列,然后执行 rbind

new <-  do.call(rbind, Map(cbind, Name = seq_along(data), data))