How to find the sum of corresponding elements in multiple vectors even if they contain NA’s in R?

R ProgrammingServer Side ProgrammingProgramming

If we have multiple vectors then the sum of the corresponding elements can be found by using rowSums functions and the vectors can be combined by using cbind so that R can easily read the corresponding elements. But if there are NA values in one or more vectors then we also need to add na.rm=TRUE argument.

Example

 Live Demo

set.seed(100)
x1<-sample(c(0,1,NA),100,replace=TRUE)
x1

Output

[1] 0 0 1 0 1 1 NA 1 1 0 1 NA 0 1 NA NA 0 1 1 NA 1 NA 1 NA 1
[26] 0 NA NA 1 0 1 NA 1 NA NA NA 0 1 NA 0 0 NA NA NA 1 1 NA NA 0 0
[51] 0 0 0 0 1 0 0 0 1 0 1 1 NA NA 1 1 1 1 0 NA 1 0 1 NA 1
[76] 1 NA NA NA 0 1 1 NA NA 0 1 NA 0 0 NA NA 0 1 1 NA 1 1 0 0 NA

Example

 Live Demo

x2<-sample(c(5,10,NA),100,replace=TRUE) x2

Output

[1] 5 10 NA 5 NA NA 10 10 NA NA 5 5 10 10 NA 10 10 5 10 5 5 5 10 5 NA
[26] 10 10 NA 5 10 5 NA NA NA NA 10 NA 10 10 10 10 5 NA 5 10 5 NA 5 10 10
[51] 5 10 5 NA 5 5 NA NA 10 NA NA 10 NA NA 10 10 5 5 10 NA 10 5 10 NA NA
[76] 10 10 NA NA 10 10 10 NA 10 NA NA NA 10 NA NA NA 5 10 NA 5 NA NA 10 5 5

Example

 Live Demo

x3<-sample(c(1:4,NA),100,replace=TRUE)
x3

Output

[1] 2 3 1 4 1 NA 2 NA 4 NA NA 4 1 3 3 4 NA 4 2 2 1 2 3 NA 3
[26] 1 4 1 3 2 3 NA 3 NA 2 4 NA NA 3 3 NA 3 2 3 3 NA 4 2 3 1
[51] 1 3 1 2 2 4 3 4 1 1 NA 2 1 4 2 2 4 3 3 4 3 4 2 4 NA
[76] 1 4 2 1 NA NA 2 2 2 2 1 1 1 3 NA 3 1 3 1 2 4 1 4 2 NA

Example

 Live Demo

x4<-sample(c(1:10,NA),100,replace=TRUE)
x4

Output

[1] 9 5 9 1 2 10 6 5 4 4 NA 6 5 2 3 7 9 5 2 2 8 1 3 5 10
[26] 10 8 7 2 4 6 4 3 3 8 10 NA 2 4 2 1 2 3 5 8 1 NA 5 4 9
[51] 8 7 8 8 4 10 3 8 1 10 9 6 9 3 4 4 8 NA 10 6 10 1 4 NA 1
[76] 5 4 NA 2 2 9 2 9 NA 8 3 10 1 6 4 3 4 4 1 1 9 NA 2 8 9

Example

rowSums(cbind(x1,x2,x3,x4),na.rm=TRUE)

Output

[1] 11 9 14 16 12 12 20 1 20 8 5 14 25 14 3 11 6 13 21 8 15 18 3 17 14
[26] 20 12 11 13 20 13 22 7 18 12 20 8 22 3 11 22 4 8 9 7 10 24 21 11 7
[51] 21 18 10 8 18 8 17 7 19 6 20 10 18 16 15 7 10 22 7 16 20 19 14 4 8
[76] 14 12 1 8 24 15 23 7 9 2 16 18 11 6 10 22 8 19 12 18 1 8 4 22 16

Example

 Live Demo

y1<-sample(c(0:9,NA),100,replace=TRUE) y1

Output

[1] 8 7 3 9 2 9 2 5 6 0 4 2 4 1 2 9 0 5 5 3 4 7 5 6 3
[26] NA 3 3 5 2 4 7 9 3 2 7 7 2 9 5 3 NA 1 3 3 2 5 8 3 9
[51] 9 6 2 2 NA NA 3 0 5 NA 6 NA 4 8 7 3 9 6 9 3 2 7 3 2 7
[76] NA 6 4 4 5 5 6 5 2 1 8 7 1 5 9 2 1 7 5 2 6 8 1 5 0

Example

 Live Demo

y2<-sample(c(16:20,NA),100,replace=TRUE) y2

Output

[1] 16 20 20 20 19 16 17 NA 19 20 18 18 16 19 20 NA 19 18 16 18 20 16 18 NA 18
[26] 16 16 NA 17 NA 16 20 17 18 16 18 20 18 18 18 17 17 16 18 17 20 18 19 19 18
[51] 19 18 18 18 16 17 17 19 NA 17 16 18 18 17 18 20 19 16 19 20 16 19 20 16 NA
[76] 19 18 17 17 16 18 16 18 19 NA NA NA 18 20 19 16 20 18 17 18 19 16 16 18 19

Example

 Live Demo

y3<-sample(c(1:10,NA),100,replace=TRUE)
y3

Output

[1] 9 2 4 4 9 10 2 2 NA 4 10 6 3 9 3 8 5 6 3 8 10 3 10 6 5
[26] 1 5 7 NA 7 6 2 6 9 9 2 7 4 2 2 3 6 9 9 5 9 8 8 9 3
[51] 9 8 1 6 8 6 9 10 1 4 6 1 7 9 9 3 1 2 7 4 9 3 8 NA NA
[76] 6 NA 9 1 6 NA 10 4 5 5 7 6 9 10 5 10 5 4 6 5 10 10 9 10 3

Example

 Live Demo

y4<-sample(c(0:1,NA),100,replace=TRUE) y4

Output

[1] 0 0 1 0 NA 1 0 0 1 1 1 1 NA 0 NA 1 0 1 1 0 NA 0 0 0 1
[26] NA 1 NA 0 1 NA 0 0 NA 1 NA 1 NA NA 0 NA 0 NA 1 0 1 0 0 1 1
[51] 1 1 NA 0 NA NA 0 NA 1 1 0 1 1 NA NA 0 NA NA 1 NA 1 1 0 1 NA
[76] 1 0 NA NA 0 0 0 0 NA NA NA 0 NA 1 0 NA NA 0 0 1 0 0 1 0 NA

Example

rowSums(cbind(y1,y2,y3,y4),na.rm=TRUE)

Output

[1] 30 28 28 23 33 11 31 25 9 13 30 32 1 23 9 31 28 36 20 19 11 34 25 34 22 [26] 24 33 20 33 30 23 32 26 3 24 20 25 25 19 23 34 12 35 25 29 30 19 32 25 8
[51] 28 25 22 33 28 25 30 5 22 29 20 12 30 30 30 37 32 25 18 12 6 28 25 30 24
[76] 30 16 37 13 26 36 12 25 34 28 36 23 26 26 29 35 11 27 26 29 25 29 29 18 32

Example

 Live Demo

z1<-rnorm(50,2,1)
z1

Output

[1] 0.9390446 1.8841026 0.8087676 0.1962055 1.4784851 1.2412525
[7] 3.6371884 2.6628671 1.9350691 1.7370759 0.9465960 2.5413368
[13] 2.4258287 2.8816722 1.3528497 2.2549404 0.2872082 1.0979457
[19] 2.8332518 0.9191813 0.9455777 3.7688646 2.4412353 2.4766955
[25] 3.6113515 1.5080660 1.5711290 0.2477241 1.7384521 2.1096380
[31] 0.7545868 2.8332222 2.8187682 3.0912341 -0.1257987 2.6147647
[37] -0.4006146 1.2134572 2.3096535 0.6461462 1.0135814 3.4684232
[43] 0.5147140 2.6046640 1.4192276 2.7918187 1.3309162 3.1853232
[49] 2.2843582 0.4706695

Example

 Live Demo

z2<-rnorm(50,5,1)
z2

Output

[1] 5.194030 3.659833 6.463705 4.058468 3.753853 7.025124 4.360630 3.764877
[9] 4.203693 5.497098 4.798063 3.839269 5.313773 3.739678 5.147129 4.646849
[17] 5.368790 3.020165 6.553668 4.956129 5.255372 5.690039 4.483388 6.234553
[25] 7.198351 6.886410 4.595667 5.211127 4.925852 6.680891 5.288206 6.860453
[33] 5.802522 5.717358 5.927209 3.892963 3.774943 4.742993 4.745586 4.863254
[41] 6.604966 3.766678 3.995291 5.265133 4.197123 3.350861 5.282620 5.661311
[49] 3.747484 5.096677

Example

 Live Demo

z3<-rnorm(50,10,1)
z3

Output

[1] 9.763367 9.580911 10.348878 9.345549 9.722785 9.969824 9.765327
[8] 11.663498 8.961884 9.606949 9.031431 10.761535 11.570366 9.555245
[15] 10.977628 10.625502 10.392293 10.128622 10.149368 9.966646 9.286093
[22] 12.026304 9.701265 9.738910 8.869799 9.065077 11.659160 10.745537
[29] 10.095464 9.371354 10.270817 10.621408 9.796881 10.455191 11.025334
[36] 9.912442 9.275351 9.334389 10.687026 10.064154 8.855830 8.800893
[43] 9.511070 11.068484 9.432597 9.525493 9.489726 10.794717 7.277489
[50] 8.777386

Example

 Live Demo

z4<-rnorm(50,12,1)
z4

Output

[1] 9.996957 10.218267 9.826439 11.906861 13.069571 11.481097 12.467620
[8] 11.889104 13.010052 11.185881 12.684369 9.749237 11.323544 11.476971
[15] 11.067891 12.924520 14.284396 11.494215 10.214767 10.580071 10.178868
[22] 13.288666 12.467862 11.423686 12.146402 11.443403 13.539518 11.176606
[29] 14.230201 10.965661 10.241278 13.162762 11.863015 10.614357 13.261041
[36] 12.370224 12.048375 13.118374 11.697336 11.376205 12.656320 11.178566
[43] 10.832501 11.015387 11.016804 11.357585 13.443464 13.807832 12.021377
[50] 12.202021

Example

rowSums(cbind(z1,z2,z3,z4),na.rm=TRUE)

Output

[1] 30.44584 31.54599 28.98250 26.92679 29.40666 28.20746 32.78253 27.98593
[9] 30.71212 30.81943 27.75039 29.63786 29.35340 25.96591 29.01252 28.83205
[17] 31.40262 29.33605 30.50771 33.15974 29.79469 28.08797 29.56919 26.35508
[25] 29.59622 28.42052 26.37487 29.12010 30.86113 27.35858 30.30758 33.52172
[33] 25.13615 28.12270 27.55533 28.59861 30.45220 30.08791 32.59381 30.67407
[41] 29.72042 29.40276 29.71063 33.81690 31.77828 34.39260 27.38295 30.76655
[49] 28.85173 27.17768
raja
Published on 09-Oct-2020 15:04:18
Advertisements