如何找到R中两个列表元素的和?
不能直接添加两个列表的list元素,但是我们可以通过取消列出元素来进行添加。为此,我们需要使用lapply函数。例如,如果我们有两个定义为x和y的列表,则可以按以下方式计算这些列表中元素的总和:
示例
lapply(seq_along(x),function(i) unlist(x[i])+unlist(y[i]))
例1
> x1<-list(rpois(150,1))> x1
输出结果
[[1]][1] 0 3 0 1 2 0 1 0 1 3 3 0 0 0 1 1 0 1 0 1 1 0 2 0 0 6 1 2 1 1 1 1 2 1 1 0 0
[38] 2 2 2 2 0 0 2 3 2 0 2 1 1 0 0 1 1 2 2 0 0 0 1 0 2 2 0 2 6 0 0 0 2 0 0 2 1
[75] 0 0 1 1 2 0 4 1 1 2 1 1 2 0 3 2 0 1 2 0 1 0 4 1 0 2 0 2 2 1 2 0 0 0 1 2 0
[112] 0 0 2 0 0 0 0 3 1 0 2 0 2 0 0 1 1 1 2 1 0 1 1 3 2 1 1 2 1 1 1 1 1 1 1 1 3
[149] 0 2
示例
> y1<-list(rpois(150,5))> y1
输出结果
[[1]][1] 2 5 6 6 7 5 7 5 8 5 3 3 6 7 4 7 4 4 6 4 10 6 5 8 7
[26] 5 4 3 8 4 10 6 8 2 8 3 6 9 3 2 9 2 1 4 5 5 7 3 5 7
[51] 3 9 1 5 2 10 1 2 6 5 4 3 4 3 2 9 4 2 4 3 5 4 3 2 8
[76] 8 4 2 6 3 3 3 3 10 8 3 9 4 6 6 5 2 10 4 3 4 8 5 4 4
[101] 4 7 4 5 2 10 9 8 5 6 6 3 4 5 10 3 4 4 3 7 5 5 7 1 9
[126] 3 6 4 9 5 5 8 6 6 5 4 5 2 6 4 6 3 7 5 7 5 5 1 1 5
示例
> lapply(seq_along(x1),function(i) unlist(x1[i])+unlist(y1[i]))
输出结果
[[1]][1] 2 8 6 7 9 5 8 5 9 8 6 3 6 7 5 8 4 5 6 5 11 6 7 8 7
[26] 11 5 5 9 5 11 7 10 3 9 3 6 11 5 4 11 2 1 6 8 7 7 5 6 8
[51] 3 9 2 6 4 12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5 2 7 8 3
[51] 3 6 8 2 7 9 4 2 3 5 4 6 5 2 8 5 10 6 6 4 9 7 5 7 7
[76] 6 6 2 2 5 6 10 6 4 4 7 6 5 5 10 10 4 4 5 3 10 1 7 8 6
[101] 4 7 4 8 6 2 3 1 7 2 8 5 5 2 2 3 5 7 7 2 3 7 4 2 8
[126] 3 5 11 5 5 3 6 3 5 1 5 4 9 5 2 3 4 9 1 6 1 12 4 6 4
$B
[1] 5 7 5 5 3 7 5 8 8 5 3 8 4 8 10 4 7 5 6 5 7 4 4 7 6
[26] 4 5 1 3 2 6 8 3 3 6 5 7 5 6 5 3 8 6 4 4 3 5 3 6 8
[51] 8 6 4 5 12 3 4 3 6 5 10 5 3 4 2 3 4 7 3 6 8 2 4 3 8
[76] 12 6 5 3 3 7 3 6 4 5 8 6 7 5 7 6 4 6 3 6 3 7 1 2 5
[101] 5 4 6 3 5 2 4 1 5 9 4 3 4 6 2 12 3 5 7 6 1 9 2 7 2
[126] 5 7 4 4 7 2 3 5 0 8 7 0 3 4 5 4 8 4 2 6 1 6 7 5 1
示例
> lapply(seq_along(x2),function(i) unlist(x2[i])+unlist(y2[i]))
输出结果
[[1]]A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16
6 2 6 3 3 5 7 8 4 6 7 8 6 2 4 5
A17 A18 A19 A20 A21 A22 A23 A24 A25 A26 A27 A28 A29 A30 A31 A32
8 9 4 3 4 5 7 5 6 7 12 6 9 6 5 9
A33 A34 A35 A36 A37 A38 A39 A40 A41 A42 A43 A44 A45 A46 A47 A48
5 7 7 9 5 8 4 7 5 7 5 4 7 5 2 8
A49 A50 A51 A52 A53 A54 A55 A56 A57 A58 A59 A60 A61 A62 A63 A64
9 3 3 8 8 2 10 11 5 2 3 5 4 6 6 3
A65 A66 A67 A68 A69 A70 A71 A72 A73 A74 A75 A76 A77 A78 A79 A80
9 5 11 7 6 5 11 7 8 8 7 6 8 2 3 6
A81 A82 A83 A84 A85 A86 A87 A88 A89 A90 A91 A92 A93 A94 A95 A96
8 15 7 5 4 8 7 5 8 10 11 6 4 6 3 14
A97 A98 A99 A100 A101 A102 A103 A104 A105 A106 A107 A108 A109 A110 A111 A112
1 7 9 7 4 7 4 8 9 3 3 2 9 4 10 5
A113 A114 A115 A116 A117 A118 A119 A120 A121 A122 A123 A124 A125 A126 A127 A128
5 2 2 5 5 9 7 2 4 7 4 3 8 4 5 11
A129 A130 A131 A132 A133 A134 A135 A136 A137 A138 A139 A140 A141 A142 A143 A144
7 9 3 9 4 6 2 6 8 9 5 4 4 4 10 2
A145 A146 A147 A148 A149 A150
7 3 13 8 6 5
[[2]]
B1 B2 B3 B4 B5 B6 B7 B8 B9 B10 B11 B12 B13 B14 B15 B16
6 8 6 6 4 8 6 10 9 6 3 9 6 10 10 5
B17 B18 B19 B20 B21 B22 B23 B24 B25 B26 B27 B28 B29 B30 B31 B32
8 7 7 7 7 5 6 9 6 5 7 2 3 2 8 11
B33 B34 B35 B36 B37 B38 B39 B40 B41 B42 B43 B44 B45 B46 B47 B48
3 3 8 5 8 5 9 6 3 10 6 6 5 3 6 4
B49 B50 B51 B52 B53 B54 B55 B56 B57 B58 B59 B60 B61 B62 B63 B64
7 9 8 6 8 6 12 4 5 4 6 6 10 6 4 5
B65 B66 B67 B68 B69 B70 B71 B72 B73 B74 B75 B76 B77 B78 B79 B80
2 4 5 7 5 7 9 2 4 7 9 13 6 7 4 5
B81 B82 B83 B84 B85 B86 B87 B88 B89 B90 B91 B92 B93 B94 B95 B96
7 4 6 4 7 9 7 8 5 7 6 5 9 4 6 3
B97 B98 B99 B100 B101 B102 B103 B104 B105 B106 B107 B108 B109 B110 B111 B112
11 4 3 5 7 4 7 3 6 2 4 2 6 9 4 4
B113 B114 B115 B116 B117 B118 B119 B120 B121 B122 B123 B124 B125 B126 B127 B128
7 8 4 14 7 5 8 6 3 10 2 9 2 5 7 5
B129 B130 B131 B132 B133 B134 B135 B136 B137 B138 B139 B140 B141 B142 B143 B144
5 9 5 4 6 2 9 8 0 4 4 6 6 8 5 2
B145 B146 B147 B148 B149 B150
7 4 6 8 6 3
例子3
> x3<-list(Rate1=rexp(30,1.5),Rate2=rexp(30,3.5))> x3
输出结果
$Rate1[1] 1.08198406 0.93432226 0.35832234 0.54181853 0.18395878 0.12118337
[7] 0.97751575 0.60592615 0.74998466 0.06824078 0.39062941 0.69376756
[13] 0.05238994 0.52151931 1.19805045 0.20881415 0.38404264 1.55547461
[19] 1.21818593 2.07793042 0.10273742 0.39657856 0.11344230 1.62549804
[25] 0.56995319 0.42592898 1.27336543 0.23423547 0.43486744 0.66305408
$Rate2
[1] 0.59293638 0.06756180 0.90668002 0.25858966 0.02585776 0.06397575
[7] 0.12895207 0.23388895 0.04266593 0.13947988 0.01986833 0.15896929
[13] 0.10892734 0.05140337 0.01440175 0.81753751 0.01076403 0.12512247
[19] 0.18719568 0.83685738 0.02082941 0.42253518 0.15404478 0.29052936
[25] 0.02342934 0.23135028 0.17910521 0.02461732 0.05052510 0.88067468
示例
> y3<-list(Rate1=rexp(30,2.57),Rate2=rexp(30,1.12))> y3
输出结果
$Rate1[1] 0.07681322 0.47038556 0.13144795 0.37913017 0.16797945 1.19891179
[7] 0.16725562 0.03104655 0.94815962 0.12819152 0.14037312 0.06082729
[13] 0.69939062 0.10002889 0.04374406 0.71316015 0.04817265 0.21396244
[19] 0.59415015 0.35806534 0.07922583 0.88672645 0.60043496 0.12122608
[25] 0.26787711 1.58494225 0.49885153 0.53077187 0.01371467 0.42030458
$Rate2
[1] 3.18396329 0.24757273 0.95512373 2.60433809 0.06030788 1.66411109
[7] 0.50855408 0.02510665 3.06849685 2.31745340 4.71355020 0.11110951
[13] 0.68447135 3.89872720 0.04109270 1.69056995 1.39894948 0.52007497
[19] 0.93378154 0.99625620 0.56953542 1.91157072 1.10670788 1.34177169
[25] 1.03726487 2.17068341 0.17688506 0.58159824 1.35985980 0.23143819
示例
> lapply(seq_along(x3),function(i) unlist(x3[i])+unlist(y3[i]))
输出结果
[[1]]Rate11 Rate12 Rate13 Rate14 Rate15 Rate16 Rate17 Rate18
1.1587973 1.4047078 0.4897703 0.9209487 0.3519382 1.3200952 1.1447714 0.6369727
Rate19 Rate110 Rate111 Rate112 Rate113 Rate114 Rate115 Rate116
1.6981443 0.1964323 0.5310025 0.7545949 0.7517806 0.6215482 1.2417945 0.9219743
Rate117 Rate118 Rate119 Rate120 Rate121 Rate122 Rate123 Rate124
0.4322153 1.7694370 1.8123361 2.4359958 0.1819633 1.2833050 0.7138773 1.7467241
Rate125 Rate126 Rate127 Rate128 Rate129 Rate130
0.8378303 2.0108712 1.7722170 0.7650073 0.4485821 1.0833587
[[2]]
Rate21 Rate22 Rate23 Rate24 Rate25 Rate26 Rate27
3.77689967 0.31513453 1.86180375 2.86292775 0.08616564 1.72808684 0.63750615
Rate28 Rate29 Rate210 Rate211 Rate212 Rate213 Rate214
0.25899560 3.11116278 2.45693328 4.73341853 0.27007880 0.79339868 3.95013057
Rate215 Rate216 Rate217 Rate218 Rate219 Rate220 Rate221
0.05549446 2.50810746 1.40971351 0.64519744 1.12097722 1.83311358 0.59036483
Rate222 Rate223 Rate224 Rate225 Rate226 Rate227 Rate228
2.33410590 1.26075266 1.63230105 1.06069421 2.40203369 0.35599026 0.60621556
Rate229 Rate230
1.41038490 1.11211287
以上是 如何找到R中两个列表元素的和? 的全部内容, 来源链接: utcz.com/z/343271.html