如何从R中的向量列表创建计数表

时间:2018-07-19 05:52:03

标签: r

我有以下列表:

my_list <- structure(list(RandomForest = c("charge.Group2", "polarizability.Group2", 
"hydrophobicity.Group1", "KR", "secondarystruct.Group1", "solventaccess.Group1", 
"Q", "hydrophobicity.Group2", "normwaalsvolume.Group1", "polarity.Group3"
), rocValues = c("KR", "Q", "KE", "KV", "charge.Group2", "SG", 
"MEK", "polarity.Group3", "V", "hydrophobicity.Group1"), ReliefF = c("KR", 
"SG", "MEK", "KV", "YH", "EP", "KY", "KYH", "PVK", "KKY"), MIC = c("solventaccess.Group1", 
"R", "P", "hydrophobicity.Group2", "Q", "polarity.Group3", "KR", 
"polarizability.Group2", "polarity.Group2", "charge.Group1")), .Names = c("RandomForest", 
"rocValues", "ReliefF", "MIC"))

my_list
#> $RandomForest
#>  [1] "charge.Group2"          "polarizability.Group2" 
#>  [3] "hydrophobicity.Group1"  "KR"                    
#>  [5] "secondarystruct.Group1" "solventaccess.Group1"  
#>  [7] "Q"                      "hydrophobicity.Group2" 
#>  [9] "normwaalsvolume.Group1" "polarity.Group3"       
#> 
#> $rocValues
#>  [1] "KR"                    "Q"                    
#>  [3] "KE"                    "KV"                   
#>  [5] "charge.Group2"         "SG"                   
#>  [7] "MEK"                   "polarity.Group3"      
#>  [9] "V"                     "hydrophobicity.Group1"
#> 
#> $ReliefF
#>  [1] "KR"  "SG"  "MEK" "KV"  "YH"  "EP"  "KY"  "KYH" "PVK" "KKY"
#> 
#> $MIC
#>  [1] "solventaccess.Group1"  "R"                    
#>  [3] "P"                     "hydrophobicity.Group2"
#>  [5] "Q"                     "polarity.Group3"      
#>  [7] "KR"                    "polarizability.Group2"
#>  [9] "polarity.Group2"       "charge.Group1"

我想做的是创建一个这样的计数表:

                     RandomForest             rocValues     ReliefF          MIC
charge.Group1          0                          0           0               1
charge.Group2          1                          1           0               0
...
YH                     0                          0           1               0

我该如何实现?

基本上,获取每个命名列表的内容并计算是否存在 总体内容上。

1 个答案:

答案 0 :(得分:4)

为此base个衬里:

table(stack(my_list))
#                        ind
#values                   RandomForest rocValues ReliefF MIC
#  charge.Group1                     0         0       0   1
#  charge.Group2                     1         1       0   0
#  EP                                0         0       1   0
#  hydrophobicity.Group1             1         1       0   0
#....