汇总具有相同列ID的行,列表中的矩阵

时间:2018-02-23 13:25:31

标签: r matrix sum

让我们说我们有以下列表,其中包含一些矩阵(可能很多,但为了简单起见,我现在只放两个)

> rel$E
      i   j   value             
 [1,] "3" "5" "0.070711136732969"  
 [2,] "3" "6" "0.0555555555555557" 
 [3,] "1" "3" "0.0178395371187627" 
 [4,] "1" "2" "0.00488002262797937"
 [5,] "2" "5" "0.0272189957856598" 
 [6,] "3" "4" "0.0438244348035453" 
 [7,] "2" "4" "0.0128664170608579" 
 [8,] "3" "3" "0"                  
 [9,] "2" "3" "0.0167431138932832" 
[10,] "2" "2" "0"                  
[11,] "4" "5" "0.0180208592355387" 
[12,] "2" "6" "0.028063474878704"  
[13,] "1" "5" "0.00937210021651717"
[14,] "1" "4" "0.0033698603568658" 
> rel$D
      i   j   value             
 [1,] "1" "3" "0.0398765637816322" 
 [2,] "1" "1" "0"                  
 [3,] "1" "4" "0.00452411512576561"
 [4,] "3" "4" "0.0193536780493677" 
 [5,] "1" "2" "0.00289466496926153"
 [6,] "2" "5" "0.0283053038069326" 
 [7,] "3" "3" "0"                  
 [8,] "3" "6" "0.0862179235688977" 
 [9,] "1" "5" "0.0242662144621697" 
[10,] "1" "6" "0.00584795321637427"
[11,] "4" "5" "0.0174208488656519" 
[12,] "2" "3" "0.0443079152300233" 
[13,] "2" "2" "0"                  
[14,] "2" "4" "0.0131264776661371" 
[15,] "3" "5" "0.0952553775375157" 

总而言之,我想要实现的是具有相同values&的矩阵的所有i列。 j

例如,对于i=3j=5,总和应为0.070711136732969 + 0.0952553775375157

但是你可以看到有一些对存在于一个但不存在于另一个矩阵中。

对于这种情况i=1j=6总和应为0 + 0.00584795321637427,因为该对不存在于第一个矩阵中。

是否有任何有效(lapply,apply)方法可以生成包含i, j, sum列的最终矩阵?但是没有使用很多for循环?我尝试用for循环接近它,但最终的代码变得难以阅读和更改。

1 个答案:

答案 0 :(得分:1)

您需要rbindaggregate,即

aggregate(value ~ i+j, do.call(rbind, l2), sum)

给出,

   i j       value
1  1 1 0.000000000
2  1 2 0.007774688
3  2 2 0.000000000
4  1 3 0.057716101
5  2 3 0.061051029
6  3 3 0.000000000
7  1 4 0.007893975
8  2 4 0.025992895
9  3 4 0.063178113
10 1 5 0.033638315
11 2 5 0.055524300
12 3 5 0.165966514
13 4 5 0.035441708
14 1 6 0.005847953
15 2 6 0.028063475
16 3 6 0.141773479

数据

dput(l2)
list(structure(c(3, 3, 1, 1, 2, 3, 2, 3, 2, 2, 4, 2, 1, 1, 5, 
6, 3, 2, 5, 4, 4, 3, 3, 2, 5, 6, 5, 4, 0.070711136732969, 0.0555555555555557, 
0.0178395371187627, 0.00488002262797937, 0.0272189957856598, 
0.0438244348035453, 0.0128664170608579, 0, 0.0167431138932832, 
0, 0.0180208592355387, 0.028063474878704, 0.00937210021651717, 
0.0033698603568658), .Dim = c(14L, 3L), .Dimnames = list(NULL, 
    c("i", "j", "value"))), structure(c(1, 1, 1, 3, 1, 2, 3, 
3, 1, 1, 4, 2, 2, 2, 3, 3, 1, 4, 4, 2, 5, 3, 6, 5, 6, 5, 3, 2, 
4, 5, 0.0398765637816322, 0, 0.00452411512576561, 0.0193536780493677, 
0.00289466496926153, 0.0283053038069326, 0, 0.0862179235688977, 
0.0242662144621697, 0.00584795321637427, 0.0174208488656519, 
0.0443079152300233, 0, 0.0131264776661371, 0.0952553775375157
), .Dim = c(15L, 3L), .Dimnames = list(NULL, c("i", "j", "value"
))))