如何将列表文件转换为矩阵

时间:2014-04-22 09:47:21

标签: r list matrix

我有一个列表文件如下:

> results
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[[1]][[1]][[1]][[1]]
[1] "1"            "inflammation" "37.5"         "A"            "B"           
[6] "F"           

[[1]][[1]][[2]]
[[1]][[1]][[2]][[1]]
[1] "1"         "Apoptosis" "37.5"      "C"         "G"         "H"        

[[1]][[1]][[3]]
[[1]][[1]][[3]][[1]]
[1] "1"      "Repair" "25"     "A"      "H"     

[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[[2]][[1]][[1]][[1]]
[1] "2"            "inflammation" "20"           "F"           

[[2]][[1]][[2]]
[[2]][[1]][[2]][[1]]
[1] "2"         "Apoptosis" "40"        "G"         "H"        

[[2]][[1]][[3]]
[[2]][[1]][[3]][[1]]
[1] "2"      "Repair" "20"     "H"     

这也是dput函数的输出:

dput(results)
list(list(list(list(c("1", "inflammation", "37.5", "A", "B", 
"F")), list(c("1", "Apoptosis", "37.5", "C", "G", "H")), list(
    c("1", "Repair", "25", "A", "H")))), list(list(list(c("2", 
"inflammation", "20", "F")), list(c("2", "Apoptosis", "40", "G", 
"H")), list(c("2", "Repair", "20", "H")))), list(list(list(c("3", 
"inflammation", "25", "F")), list(c("3", "Apoptosis", "25", "C"
)), list(c("3", "Repair", "0")))), list(list(list(c("4", "inflammation", 
"50", "A", "B", "F")), list(c("4", "Apoptosis", "33.3333333333333", 
"G", "H")), list(c("4", "Repair", "33.3333333333333", "A", "H"
)))))

然后我想制作一个像这样的矩阵

    Number         pathway              wight     genes
    1                inflammation       37.5      A, B, F           
    1                Apoptosis          37.5      C, G, H
    1                Repair             25         A, H
    2                inflammation       20          F           
    2                Apoptosis          40         G, H
    2                Repair             20          H

这有什么诀窍吗?基因列包括不同数量的基因。

2 个答案:

答案 0 :(得分:2)

首先,您应该unlist几次结果。然后你必须paste基因在一起,最后你可以rbind数据。这是这样的样子。

lst <- unlist(unlist(unlist(results, recursive=FALSE), recursive=FALSE), recursive=FALSE)
df <- do.call(rbind, lapply(lst, 
       function(x){
         data.frame(Number=as.numeric(x[1]), 
                    pathway=x[2], 
                    weight=as.numeric(x[3]), 
                    genes=paste(x[4:max(4, length(x))], collapse=", "))
       }))
df

##    Number      pathway   weight   genes
## 1       1 inflammation 37.50000 A, B, F
## 2       1    Apoptosis 37.50000 C, G, H
## 3       1       Repair 25.00000    A, H
## 4       2 inflammation 20.00000       F
## 5       2    Apoptosis 40.00000    G, H
## 6       2       Repair 20.00000       H
## 7       3 inflammation 25.00000       F
## 8       3    Apoptosis 25.00000       C
## 9       3       Repair  0.00000      NA
## 10      4 inflammation 50.00000 A, B, F
## 11      4    Apoptosis 33.33333    G, H
## 12      4       Repair 33.33333    A, H

答案 1 :(得分:0)

我可能会像上面的 shadow 那样处理手头的问题,但这里也是一个替代的递归实现,它不能修复不列表的数量,即它可以处理不同的深度列表清单。 它只是遍历树,直到它找到 list 以外的元素并对其执行一些指定的格式,最后全部绑定到一个矩阵:

recurse.format <- function(
    x, 
    format = function(z) { c(z[1:3], ifelse(length(z)>3, paste(z[4:length(z)], collapse=","), NA)) } 
){
    if(class(x) == "list"){
        do.call("rbind", lapply(x, FUN=recurse.format))
    }else{
        format(x)
    }
}

mat <- recurse.format(results)
colnames(mat) <- c("Number", "Pathway", "Weight", "Genes")
print(mat)


> print(mat)
      Number Pathway        Weight             Genes  
 [1,] "1"    "inflammation" "37.5"             "A,B,F"
 [2,] "1"    "Apoptosis"    "37.5"             "C,G,H"
 [3,] "1"    "Repair"       "25"               "A,H"  
 [4,] "2"    "inflammation" "20"               "F"    
 [5,] "2"    "Apoptosis"    "40"               "G,H"  
 [6,] "2"    "Repair"       "20"               "H"    
 [7,] "3"    "inflammation" "25"               "F"    
 [8,] "3"    "Apoptosis"    "25"               "C"    
 [9,] "3"    "Repair"       "0"                NA     
[10,] "4"    "inflammation" "50"               "A,B,F"
[11,] "4"    "Apoptosis"    "33.3333333333333" "G,H"  
[12,] "4"    "Repair"       "33.3333333333333" "A,H"