【问题标题】:How to convert rownames to columns in a list of elements如何将行名转换为元素列表中的列
【发布时间】:2021-06-04 08:29:38
【问题描述】:

我有一个名为 results1 的元素列表。每个元素都有一个列名和行名。这些元素是向量。

class(results1)
[1] "list"
> class(results1[[1]])
[1] "numeric"
is.vector(results1[[1]])
[1] TRUE

results1[[1]]
      ..a15.pdf       ..a17.pdf       ..a18.pdf       ..a21.pdf 
     1.27679608      1.05090176      1.51820192      2.30296037 

我想将每个向量转换为包含两列的数据框,其中行名(上面表示为 ..a15.pdf 等)是其中一列。我可以用一个单独的元素/向量来做到这一点:

j<-as.data.frame(results1[[1]])
j<-cbind(j,names=rownames(j))
rownames(j) <- c()

但我无法为整个列表实现它。我试过了

results1<-lapply(results1, function(x)cbind(x, names=rownames(x)))

我可以看到两列

results1[[1]]
                           x
..a15.pdf         1.27679608
..a17.pdf         1.05090176
..a18.pdf         1.51820192

但显然这不会创建数据框。

is.data.frame(results1[[1]])
[1] FALSE

据我了解,..15pdf 不是一列

results1[[1]][,1]
      ..a15.pdf       ..a17.pdf       ..a18.pdf       ..a21.pdf 
     1.27679608      1.05090176      1.51820192      2.30296037 

然后我尝试先将我的向量转换为数据帧:

results1<-lapply(results1, function(x)as.data.frame(x[1]))
results1<-lapply(results1, function(x){as.data.frame(x[1])})

但它没有工作。 如上所述,我想将每个向量的行名转换为一列并删除它们。输出应该是一个数据帧列表,每个 df 有两列。如果您需要名称,则 rownames 列应命名为“名称”。谢谢。 这是我列表的前两个向量的 dput

dput(results1[c(1,2)])
list(c(..a15.pdf = 1.27679607834331, ..a17.pdf = 1.05090175857491, 
..a18.pdf = 1.51820192474905, ..a21.pdf = 2.30296037386815, ..a2TTT.pdf = 1.48568731934637, 
..a5.pdf = 0.493713103224402, ..B11.pdf = 1.02705905465749, ..B12.pdf = 0.999747360884078, 
..B13.pdf = 2.40828101927852, ..B22.pdf = 0.695152132033603, 
..B24.pdf = 2.1436001615064, ..B4.pdf = 2.25444037842867, ..B7.pdf = 0.909773940025014, 
..B8.pdf = 1.14837173756827, `..cw10-1.pdf` = -1.36323271003293, 
`..cw15-1TTT.pdf` = 0.341428535787024, `..cw17-1.pdf` = -0.786878348480425, 
..cw18.pdf = 0.793720472787986, ..cw3.pdf = -1.57831038567642, 
..cw4.pdf = 0.277733503122777, ..cw7_1TTT.pdf = -0.0364645818969112, 
`..cw13-1.pdf` = -18.336668416705), c(..a15.pdf = 2.096687569578, 
..a17.pdf = 2.19826038300833, ..a18.pdf = 1.91814204277357, ..a21.pdf = 0.801448541154512, 
..a2TTT.pdf = 2.16169560949165, ..a5.pdf = 1.48585130705963, 
..B11.pdf = 0.95126061691997, ..B12.pdf = 1.93116618236938, ..B13.pdf = 1.92555316191766, 
..B22.pdf = 1.00560861920225, ..B24.pdf = 2.91129684208931, ..B4.pdf = 2.75687804718002, 
..B7.pdf = 1.31164431967781, ..B8.pdf = 2.22449059765255, `..cw10-1.pdf` = -1.22629519335285, 
`..cw15-1TTT.pdf` = 1.31168579553008, `..cw17-1.pdf` = -17.5786422399896, 
..cw18.pdf = 1.25323523754693, ..cw3.pdf = -0.754445550651364, 
..cw4.pdf = 0.555577381430987, ..cw7_1TTT.pdf = 0.577850999404076, 
`..cw13-1.pdf` = -34.2662973287062))

【问题讨论】:

    标签: r


    【解决方案1】:

    我们可以使用mapenframe

    library(purrr)
    library(tibble)
    map(results1, enframe)
    

    -输出

    [[1]]
    # A tibble: 22 x 2
       name        value
       <chr>       <dbl>
     1 ..a15.pdf   1.28 
     2 ..a17.pdf   1.05 
     3 ..a18.pdf   1.52 
     4 ..a21.pdf   2.30 
     5 ..a2TTT.pdf 1.49 
     6 ..a5.pdf    0.494
     7 ..B11.pdf   1.03 
     8 ..B12.pdf   1.00 
     9 ..B13.pdf   2.41 
    10 ..B22.pdf   0.695
    # … with 12 more rows
    
    [[2]]
    # A tibble: 22 x 2
       name        value
       <chr>       <dbl>
     1 ..a15.pdf   2.10 
     2 ..a17.pdf   2.20 
     3 ..a18.pdf   1.92 
     4 ..a21.pdf   0.801
     5 ..a2TTT.pdf 2.16 
     6 ..a5.pdf    1.49 
     7 ..B11.pdf   0.951
     8 ..B12.pdf   1.93 
     9 ..B13.pdf   1.93 
    10 ..B22.pdf   1.01 
    # … with 12 more rows
    

    【讨论】:

      【解决方案2】:

      在每个 list 中创建一个数据框并删除行名 -

      lapply(results1, function(x) {
        tmp <- data.frame(names=names(x), x)
        rownames(tmp) <- NULL
        tmp
      })
      
      #[[1]]
      #             names            x
      #1        ..a15.pdf   1.27679608
      #2        ..a17.pdf   1.05090176
      #3        ..a18.pdf   1.51820192
      #4        ..a21.pdf   2.30296037
      #5      ..a2TTT.pdf   1.48568732
      #6         ..a5.pdf   0.49371310
      #7        ..B11.pdf   1.02705905
      #8        ..B12.pdf   0.99974736
      #...
      #...
      
      #[[2]]
      #             names           x
      #1        ..a15.pdf   2.0966876
      #2        ..a17.pdf   2.1982604
      #3        ..a18.pdf   1.9181420
      #4        ..a21.pdf   0.8014485
      #5      ..a2TTT.pdf   2.1616956
      #...
      #...
      

      更短的选择是 -

      lapply(results1, stack)
      

      【讨论】:

      • 谢谢。有用。你可能还记得你昨天试图盲目地这样做。昨天在您的尝试中有所不同的位:mat
      • 是的,我确实记得。昨天,我假设你有矩阵列表,但实际上你有一个数字向量列表。
      【解决方案3】:

      这个问题可能类似于this。 @hrbrmstr 的答案很简洁,可以使用 rownames_to_column() 函数将行名转换为列。

      library(tibble)
      df <- rownames_to_column(.data, var = "rowname")
      

      【讨论】:

        猜你喜欢
        • 2022-11-17
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多