【问题标题】:Set column to rownames for a list of sublist of dataframes in r [duplicate]将列设置为行名以获取 r [重复] 中的数据框子列表
【发布时间】:2020-07-17 03:29:21
【问题描述】:

我有一个包含 6 个子列表的列表,每个子列表包含 3 个数据框。

我想将数据帧的行名设置为该数据报中的一列。

list_1 <- list(df1, df2, df3)
list_2 <- lapply(list_1, function(x) split(x, x$Type))

我尝试用lapply重命名数据框的行

lapply(list_2, function(x) column_to_rownames(x, var=x$Rows))

print(df1)
      Rows       A
1    Baseline    4
2    Sample1     5
3    Sample2     8
4    Sample3     6
5    AASHTO      9
6    Mean        3

print(df2)
      Rows       A
1    Baseline    4
2    Sample1     7
3    Sample2     8
4    Sample3     6
5    AASHTO      4
6    Mean        3

print(df3)
      Rows       A
1    Baseline    3
2    Sample1     5
3    Sample2     6
4    Sample3     6
5    AASHTO      5
6    Mean        3

【问题讨论】:

    标签: r


    【解决方案1】:

    您在list_2 中有嵌套列表。试试unlisting 一级

    list_3 <- lapply(unlist(list_2, recursive = FALSE), function(x) 
                   tibble::column_to_rownames(x, var= "Rows"))
    

    或仅使用基础 R :

    list_3 <- lapply(unlist(list_2, recursive = FALSE), function(x) 
                     {rownames(x) <- x$Rows;x$Rows <- NULL;x})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2020-12-16
      • 2018-12-14
      相关资源
      最近更新 更多