R:按列拆分列表并转换为新的data.frames

时间:2015-07-21 14:47:33

标签: r list split dataframe

我在名为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.frames?例如。创建一个名为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

依旧......

4 个答案:

答案 0 :(得分: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”中拥有所有你需要的......但是你可能需要这样做才有正当理由;)< / p>

答案 1 :(得分:0)

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

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))

转置它

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

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