【问题标题】:R: Split list by columns and convert into new data.framesR:按列拆分列表并转换为新的data.frames
【发布时间】:2015-07-21 14:47:32
【问题描述】:

我在名为 data 的列表中有以下示例数据

data <-    structure(list(`1.1` = structure(list(id = structure(1, .Dim = c(1L, 
    1L)), Sample = structure("Test1", .Dim = c(1L, 1L)), Add = structure("T", .Dim = c(1L, 
    1L))), .Names = c("id", "Sample", "Add")), `2.1` = structure(list(
        id = structure(5, .Dim = c(1L, 1L)), Sample = structure("Test2", .Dim = c(1L, 
        1L)), Add = structure("A", .Dim = c(1L, 1L))), .Names = c("id", 
    "Sample", "Add")), `3.1` = structure(list(id = structure(7, .Dim = c(1L, 
    1L)), Sample = structure("Test3", .Dim = c(1L, 1L)), Add = structure("D", .Dim = c(1L, 
    1L))), .Names = c("id", "Sample", "Add")), `4.1` = structure(list(
        id = structure(12, .Dim = c(1L, 1L)), Sample = structure("Test4", .Dim = c(1L, 
        1L)), Add = structure("Z", .Dim = c(1L, 1L))), .Names = c("id", 
    "Sample", "Add")), `5.1` = structure(list(id = structure(17, .Dim = c(1L, 
    1L)), Sample = structure("Test12", .Dim = c(1L, 1L)), Add = structure("E", .Dim = c(1L, 
    1L))), .Names = c("id", "Sample", "Add"))), .Names = c("1.1", 
    "2.1", "3.1", "4.1", "5.1"), row.names = c("id", "Sample", "Add"
    ), class = "data.frame")

看起来像这样:

 data
         1.1   2.1   3.1   4.1    5.1
id         1     5     7    12     17
Sample Test1 Test2 Test3 Test4 Test12
Add        T     A     D     Z      E

如何根据 ID 号将此列表按列拆分为多个 data.frame?例如。是否创建了一个名为 data.ID1 的 data.frame 和一个名为 data.ID5 的 data.frame 和一个名为 data.ID 7 等的 data.frame(参见下面的示例)? data.frame 的名称应与 ID 号相同。我的列表包含大约 700 个不同的 ID 和数据...

data.ID1
id      1
Sample  Test1
Add     T

data.ID5
id      5
Sample  Test2
Add     A

data.ID7
id      7
Sample  Test3
Add     D

等等……

【问题讨论】:

  • id 值是否唯一?
  • 是的,它们是独一无二的。
  • 看看你想要的输出,你想保持相同的格式(即 id、Sample、Add as row.names)......我是正确的还是你想把它们变成列名字?
  • 是的,情况正是如此。行名称是 id、Sample 和 Add。

标签: r list split dataframe


【解决方案1】:

这是一个可能的解决方案:

lst <- lapply(1:ncol(data),function(c) return(data[,c,drop=F]))
names(lst) <- lapply(data,function(col) return(paste0('data.ID',col$id)))

# here you have data.ID1, data.ID2 etc inside a lst, 
# you can have access to them simply using: lst$data.ID1, lst$data.ID2 etc.
# but if you REALLY want to add these variables in the environment, 
# continue to the next loop

for(nm in names(lst)){
  assign(nm,lst[[nm]])
}

请注意,最好不要使用assign,因为正如上面评论中所述,您在列表对象“lst”中拥有所需的一切......但也许您需要这样做是有正当理由的;)

【讨论】:

  • 已编辑:在 data.frame 名称中忘记了“ID”
【解决方案2】:

这是一个足够接近的解决方案,试试

coln <- lapply(data[1,],as.character)
colnames(data) <- paste(rep("data.TD",ncol(data)),coln,sep="")
attach(data)

然后,当您需要通过数据 id 调用某个列时,您可以这样做:

data.frame(data.TD7)

输出:

  id Sample Add
1  7  Test3   D

或者你可以使用t(data.frame(data.TD7))转置它

【讨论】:

    【解决方案3】:

    我认为这三行可以解决你的问题:

    newdata <- apply(data, 2, function(x) { y = as.data.frame(x=unlist(x)) })
    newname <- paste("data.ID", unlist(data[1, ]), sep="")
    names(newdata) <- newname
    newdata
    

    新数据是包含所需数据帧的列表

    【讨论】:

      【解决方案4】:
      data <- structure(list(
        `1.1` = structure(list(id = structure(1, .Dim = c(1L, 1L)),
                               Sample = structure("Test1", .Dim = c(1L, 1L)),
                               Add = structure("T", .Dim = c(1L, 1L))),
                          .Names = c("id", "Sample", "Add")),
        `2.1` = structure(list(id = structure(5, .Dim = c(1L, 1L)),
                               Sample = structure("Test2", .Dim = c(1L, 1L)),
                               Add = structure("A", .Dim = c(1L, 1L))),
                          .Names = c("id", "Sample", "Add")),
        `3.1` = structure(list(id = structure(7, .Dim = c(1L, 1L)),
                               Sample = structure("Test3", .Dim = c(1L, 1L)),
                               Add = structure("D", .Dim = c(1L, 1L))),
                          .Names = c("id", "Sample", "Add")),
        `4.1` = structure(list(id = structure(12, .Dim = c(1L, 1L)),
                               Sample = structure("Test4", .Dim = c(1L, 1L)),
                               Add = structure("Z", .Dim = c(1L, 1L))),
                          .Names = c("id", "Sample", "Add")),
        `5.1` = structure(list(id = structure(17, .Dim = c(1L, 1L)),
                               Sample = structure("Test12", .Dim = c(1L, 1L)),
                               Add = structure("E", .Dim = c(1L, 1L))),
                          .Names = c("id", "Sample", "Add"))),
        .Names = c("1.1", "2.1", "3.1", "4.1", "5.1"),
        row.names = c("id", "Sample", "Add"), class = "data.frame")
      
      data
      # 1.1   2.1   3.1   4.1    5.1
      # id         1     5     7    12     17
      # Sample Test1 Test2 Test3 Test4 Test12
      # Add        T     A     D     Z      E
      
      data2 <- as.data.frame(apply(data, 1, unlist))
      data2
      # id Sample Add
      # 1.1  1  Test1   T
      # 2.1  5  Test2   A
      # 3.1  7  Test3   D
      # 4.1 12  Test4   Z
      # 5.1 17 Test12   E
      
      data2 <- split(x = data2, f = data2$id)
      data2
      # $`1`
      # id Sample Add
      # 1.1  1  Test1   T
      # 
      # $`12`
      # id Sample Add
      # 4.1 12  Test4   Z
      # 
      # $`17`
      # id Sample Add
      # 5.1 17 Test12   E
      # 
      # $`5`
      # id Sample Add
      # 2.1  5  Test2   A
      # 
      # $`7`
      # id Sample Add
      # 3.1  7  Test3   D
      
      data2 <- lapply(data2, function(x){
        as.data.frame(t(x))
      })
      data2
      # $`1`
      # 1.1
      # id         1
      # Sample Test1
      # Add        T
      # 
      # $`12`
      # 4.1
      # id        12
      # Sample Test4
      # Add        Z
      # 
      # $`17`
      # 5.1
      # id         17
      # Sample Test12
      # Add         E
      # 
      # $`5`
      # 2.1
      # id         5
      # Sample Test2
      # Add        A
      # 
      # $`7`
      # 3.1
      # id         7
      # Sample Test3
      # Add        D
      

      【讨论】:

      • Edit 你的答案并添加一些解释。仅代码答案被视为低质量
      猜你喜欢
      • 2019-12-18
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2020-05-03
      • 2022-07-30
      相关资源
      最近更新 更多