R:如何将结构化列表导出为ex​​cel

时间:2012-11-19 12:38:36

标签: r excel list export

这应该非常简单,但我在unlistmatrix的努力失败了。

如何将以下列表导出到保留相同结构的Excel工作表中?

list(structure(list(rob = c(0.500395231401348, 0.839314035292317, 
0.710466394967634, 0.61574235723249)), .Names = "rob", row.names = c(NA, 
4L), class = "data.frame"), structure(list(rob = c(0.66163340478379, 
0.591092739290417, 0.554484883310407, 0.78199375944331, 0.489388775242085
)), .Names = "rob", row.names = c(NA, 5L), class = "data.frame"), 
    structure(list(rob = c(0.697897964659196, 0.480394119894312, 
    0.514294379103359, 0.626971885076273, 0.77938643423231, 0.618135503224601
    )), .Names = "rob", row.names = c(NA, 6L), class = "data.frame"))

谢谢!

1 个答案:

答案 0 :(得分:8)

您可以通过以下方式完成:

  1. NA填充每个向量以使所有向量具有相同的长度
  2. 使用do.call(rbind, ...)创建数据框
  3. 像这样:

    xx <- lapply(x, unlist)
    max <- max(sapply(xx, length))
    do.call(rbind, lapply(xx, function(z)c(z, rep(NA, max-length(z)))))
    
              rob1      rob2      rob3      rob4                    
    [1,] 0.5003952 0.8393140 0.7104664 0.6157424        NA        NA
    [2,] 0.6616334 0.5910927 0.5544849 0.7819938 0.4893888        NA
    [3,] 0.6978980 0.4803941 0.5142944 0.6269719 0.7793864 0.6181355
    

    然后,使用write.table()或您最喜欢的excel方法导出是一个简单的问题。