【问题标题】:Write a list of lists to a table, with the names of each list as a column?将列表列表写入表格,每个列表的名称作为一列?
【发布时间】:2011-09-01 11:13:26
【问题描述】:

我有一个关于如何将列表写入文件的相当基本的问题。 我有一个由 Mfuzz acore 函数生成的列表,其中列出了我在 20 个集群中的每个集群中的所有探针的名称,格式如下:

[[1]]
         NAME  MEM.SHIP
ILMN_X ILMN_X 0.9993195

.
.
.
[[20]]
         NAME  MEM.SHIP
ILMN_Y ILMN_Y 0.9982345

我想将其转换为数据框,并最终转换为将列表编号作为列包含在内的输出文件;

像这样:

CLUSTER NAME    MEM.SHIP
      1 ILMN_X 0.9993196
      .
      .
      .
     20 ILMN_Y 0.9982345

CLUSTER 列指示探针属于哪个子列表。每个探针名称可以属于多个子列表。 我已经尝试过不同的事情,比如在其他帖子中使用 plyr 的建议,但我总是只得到一个所有变量的列表,而没有指明它们属于哪个子列表。

谢谢!

【问题讨论】:

    标签: list r


    【解决方案1】:

    如果您的原始列表称为clstrs,我相信这是一种解决方案:

    do.call(rbind, lapply(seq_along(clstrs), function(i){
      data.frame(CLUSTER=i, clstrs[[i]])
    }))
    

    【讨论】:

    • 好吧,几乎没有什么出色的。但如果它有效,您可以将我的答案标记为有用(“分数”上方的箭头)和/或解决方案(绿色标记)
    【解决方案2】:

    这是另一种给猫剥皮的方法。

    # make some sample data
    my.df <- data.frame(num = 1:10, val = runif(10))
    my.list <- list(my.df, my.df, my.df, my.df, my.df, my.df)
    # build index - count the number of rows in each list element that will be 
    # used to designate the rows based on their previous list affiliation
    index <- lapply(my.list, nrow)
    index <- rep(1:length(index), times = index)
    # from here on it's basically what Nick did. rbind everything together and
    # put some lipstick on and voila
    my.out <- do.call("rbind", my.list)
    my.out$index <- index
    #or
    my.out <- cbind(my.out, index)
    

    我有几分钟的空闲时间,所以我对每个数据帧使用 10e5 行进行了快速基准测试。

    我的解决方案$index:

       user  system elapsed 
       0.81    0.27    1.08
    

    cbind 的解决方案:

       user  system elapsed 
      19.92    0.42   20.38 
    

    尼克的解决方案:

       user  system elapsed 
       1.04    0.26    1.31 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-20
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      相关资源
      最近更新 更多