如何合并列表中的框架?

时间:2015-10-11 20:02:50

标签: r list rbind

我的列表data包含10个元素(Stock 1..10),每个元素都是这样的:

test <- data$Stock1
> head(test)
    names   date_tmp efficiency
1  Stock1 2010-11-04  0.5674100
11 Stock1 2010-11-05  0.2945661
21 Stock1 2010-11-08  0.6318756
31 Stock1 2010-11-09  0.3203930
41 Stock1 2010-11-10  0.8075944
51 Stock1 2010-11-11  1.0000000

我想合并此列表中所有帧的列。我有以下代码,我从其他地方改编:

new_frame <- do.call(cbind,Map(cbind, lapply(data, as.data.frame)))

我的问题是最终结果看起来非常混乱:

  Stock1.names Stock1.date_tmp Stock1.efficiency Stock10.names Stock10.date_tmp Stock10.efficiency Stock2.names
1        Stock1      2010-11-04         0.5674100       Stock10       2010-11-04          1.0000000       Stock2
11       Stock1      2010-11-05         0.2945661       Stock10       2010-11-05          1.0000000       Stock2
21       Stock1      2010-11-08         0.6318756       Stock10       2010-11-08          1.0000000       Stock2
31       Stock1      2010-11-09         0.3203930       Stock10       2010-11-09          1.0000000       Stock2
41       Stock1      2010-11-10         0.8075944       Stock10       2010-11-10          0.0000000       Stock2
51       Stock1      2010-11-11         1.0000000       Stock10       2010-11-11          0.8065724       Stock2
   Stock2.date_tmp Stock2.efficiency Stock3.names Stock3.date_tmp Stock3.efficiency Stock4.names Stock4.date_tmp
1       2010-11-04                 1       Stock3      2010-11-04                 1       Stock4      2010-11-04
11      2010-11-05                 1       Stock3      2010-11-05                 0       Stock4      2010-11-05
21      2010-11-08                 1       Stock3      2010-11-08                 1       Stock4      2010-11-08
31      2010-11-09                 0       Stock3      2010-11-09                 1       Stock4      2010-11-09
41      2010-11-10                 1       Stock3      2010-11-10                 1       Stock4      2010-11-10
51      2010-11-11                 1       Stock3      2010-11-11                 1       Stock4      2010-11-11
   Stock4.efficiency Stock5.names Stock5.date_tmp Stock5.efficiency Stock6.names Stock6.date_tmp
1                  1       Stock5      2010-11-04         0.5218197       Stock6      2010-11-04
11                 1       Stock5      2010-11-05         0.6762827       Stock6      2010-11-05
21                 1       Stock5      2010-11-08         0.7991099       Stock6      2010-11-08
31                 1       Stock5      2010-11-09         0.5613898       Stock6      2010-11-09
41                 1       Stock5      2010-11-10         0.5531570       Stock6      2010-11-10
51                 1       Stock5      2010-11-11         0.8151261       Stock6      2010-11-11
   Stock6.efficiency Stock7.names Stock7.date_tmp Stock7.efficiency Stock8.names Stock8.date_tmp
1          0.5498426       Stock7      2010-11-04                 1       Stock8      2010-11-04
11         0.5534652       Stock7      2010-11-05                 1       Stock8      2010-11-05
21         0.3897874       Stock7      2010-11-08                 1       Stock8      2010-11-08
31         0.4405610       Stock7      2010-11-09                 1       Stock8      2010-11-09
41         0.8180463       Stock7      2010-11-10                 1       Stock8      2010-11-10
51         0.1491688       Stock7      2010-11-11                 1       Stock8      2010-11-11
   Stock8.efficiency Stock9.names Stock9.date_tmp Stock9.efficiency
1                  0       Stock9      2010-11-04         0.2756548
11                 1       Stock9      2010-11-05         0.5204240
21                 1       Stock9      2010-11-08         0.0000000
31                 1       Stock9      2010-11-09         0.7940347
41                 1       Stock9      2010-11-10         0.6393697
51                 1       Stock9      2010-11-11         0.0000000
> 

我想只有一列包含日期和效率得分行名为&#34; Stock1 ... 10&#34;。我认为从上面发布的循环函数有一种更有效的方法,而不是试图从新的数据帧中删除额外的东西。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

只需尝试使用rbind:

new_frame <- data.frame(do.call(rbind,data),row.names = NULL)

你会得到这样的东西:

> new_frame
    names   date_tmp efficiency
1  Stock1 2013-01-01  0.5450818
2  Stock1 2013-01-01  0.6710206
...
9  Stock1 2013-01-01  0.5147114
...
18 Stock2 2013-02-01  0.6650112
19 Stock3 2013-03-01  0.6624025