How to find the sum of division in R if zero exists in the vectors?

To find the sum of division if zero exists in the vectors, we need to assign NA to zeros in both the vectors and then use the sum function with na.rm set to TRUE. For example, if we have two vectors x and y that contains some zeros then we can divide x by y using the below commands −

x[x==0]<-NA
y[y==0]<-NA
sum(x/y,na.rm=TRUE)

Example

Live Demo

> x<-sample(0:9,100,replace=TRUE)
> x

Output

[1] 3 3 5 1 7 1 6 2 2 1 4 6 0 6 3 1 2 9 3 2 6 7 7 6 7 4 1 8 3 6 6 2 6 4 0 4 3
[38] 9 1 3 4 0 5 4 6 0 3 5 3 2 4 1 2 2 6 5 6 2 8 7 2 0 1 4 2 9 9 3 0 9 8 3 8 6
[75] 0 4 8 1 6 5 9 4 8 2 3 9 9 6 1 9 0 0 5 1 6 7 4 6 8 5

Example

Live Demo

> y<-sample(0:9,100,replace=TRUE)
> y

Output

[1] 1 5 3 1 9 1 3 8 9 0 1 7 3 2 3 3 2 9 3 1 9 5 5 2 5 4 4 7 4 5 9 1 9 9 4 2 3
[38] 0 5 1 9 1 8 7 1 9 3 7 9 9 7 1 3 5 7 9 0 0 1 9 2 3 8 2 0 4 6 7 7 2 3 5 9 5
[75] 4 0 4 6 3 4 8 3 9 9 9 3 5 6 1 2 6 5 5 2 1 6 3 2 1 1

Example

> sum(x/y)
[1] Inf
> x[x==0]<-NA
> y[y==0]<-NA
> x

Output

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

Example

> y

Output

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

Example

> sum(x/y,na.rm=TRUE)

Output

[1] 133.4123

Example

Live Demo

> x1<-sample(0:9,20,replace=TRUE)
> x1

Output

[1] 4 4 8 5 7 4 6 6 4 7 0 0 2 9 0 4 8 9 8 4

Example

Live Demo

> y1<-sample(0:9,20,replace=TRUE)
> y1

Output

[1] 6 2 0 7 1 1 3 5 9 1 1 7 1 6 4 5 1 5 7 4

Example

> x1[x1==0]<-NA
> y1[y1==0]<-NA
> x1

Output

[1] 4 4 8 5 7 4 6 6 4 7 NA NA 2 9 NA 4 8 9 8 4

Example

> y1

Output

[1] 6 2 NA 7 1 1 3 5 9 1 1 7 1 6 4 5 1 5 7 4


Example

> sum(x1/y1,na.rm=TRUE)

Output

[1] 41.26825

Example

Live Demo

> x2<-sample(0:50,200,replace=TRUE)
> x2

Output

[1] 42 2 1 28 41 39 45 12 35 41 4 3 40 15 6 28 27 10 4 14 17 35 31 6 9
[26] 29 35 16 2 20 44 0 28 8 0 28 25 4 3 13 4 42 11 39 41 41 6 48 49 14
[51] 44 24 14 19 12 10 43 22 21 21 0 50 45 13 43 14 24 16 17 32 31 12 13 33 31
[76] 0 33 41 30 3 17 4 41 29 40 40 9 26 39 22 4 12 45 32 5 39 39 26 20 21
[101] 9 12 4 2 47 31 1 47 0 28 40 3 17 28 14 44 2 38 6 35 23 2 8 30 0
[126] 49 8 36 15 10 28 40 15 22 26 9 16 4 26 26 30 30 28 14 14 44 37 11 14 19
[151] 21 19 50 8 27 1 14 17 25 46 50 42 4 16 14 40 26 40 13 8 40 13 26 42 23
[176] 29 4 6 33 34 30 20 5 39 0 36 6 12 40 27 36 2 20 32 32 41 11 21 34 34

Example

Live Demo

> y2<-sample(0:50,200,replace=TRUE)
> y2

Output

[1] 38 27 44 10 24 6 43 34 43 23 8 31 49 32 24 43 47 14 3 27 28 25 11 6 12
[26] 28 24 24 0 22 14 24 8 48 46 26 15 20 3 8 49 18 47 47 48 11 44 41 25 44
[51] 29 48 40 44 15 13 1 14 50 47 49 31 10 35 39 14 11 13 24 29 44 35 45 38 23
[76] 11 24 43 18 23 10 47 2 39 37 30 44 39 7 5 5 37 10 23 19 9 22 46 17 34
[101] 39 21 18 47 27 43 27 22 32 8 5 46 40 17 43 21 19 35 24 47 18 33 41 50 48
[126] 21 21 24 9 13 47 38 34 25 12 2 1 47 47 3 16 29 22 2 44 3 12 50 25 50
[151] 2 9 26 50 1 12 49 23 9 12 8 20 27 38 12 26 19 0 29 40 34 15 1 19 5
[176] 46 27 14 19 9 41 50 41 24 44 42 1 38 42 5 21 50 0 9 13 47 36 39 7 15

Example

> x2[x2==0]<-NA
> y2[y2==0]<-NA
> x2

Output

[1] 42 2 1 28 41 39 45 12 35 41 4 3 40 15 6 28 27 10 4 14 17 35 31 6 9
[26] 29 35 16 2 20 44 NA 28 8 NA 28 25 4 3 13 4 42 11 39 41 41 6 48 49 14
[51] 44 24 14 19 12 10 43 22 21 21 NA 50 45 13 43 14 24 16 17 32 31 12 13 33 31
[76] NA 33 41 30 3 17 4 41 29 40 40 9 26 39 22 4 12 45 32 5 39 39 26 20 21
[101] 9 12 4 2 47 31 1 47 NA 28 40 3 17 28 14 44 2 38 6 35 23 2 8 30 NA
[126] 49 8 36 15 10 28 40 15 22 26 9 16 4 26 26 30 30 28 14 14 44 37 11 14 19
[151] 21 19 50 8 27 1 14 17 25 46 50 42 4 16 14 40 26 40 13 8 40 13 26 42 23
[176] 29 4 6 33 34 30 20 5 39 NA 36 6 12 40 27 36 2 20 32 32 41 11 21 34 34

Example

> y2

Output

[1] 38 27 44 10 24 6 43 34 43 23 8 31 49 32 24 43 47 14 3 27 28 25 11 6 12
[26] 28 24 24 NA 22 14 24 8 48 46 26 15 20 3 8 49 18 47 47 48 11 44 41 25 44
[51] 29 48 40 44 15 13 1 14 50 47 49 31 10 35 39 14 11 13 24 29 44 35 45 38 23
[76] 11 24 43 18 23 10 47 2 39 37 30 44 39 7 5 5 37 10 23 19 9 22 46 17 34
[101] 39 21 18 47 27 43 27 22 32 8 5 46 40 17 43 21 19 35 24 47 18 33 41 50 48
[126] 21 21 24 9 13 47 38 34 25 12 2 1 47 47 3 16 29 22 2 44 3 12 50 25 50
[151] 2 9 26 50 1 12 49 23 9 12 8 20 27 38 12 26 19 NA 29 40 34 15 1 19 5
[176] 46 27 14 19 9 41 50 41 24 44 42 1 38 42 5 21 50 NA 9 13 47 36 39 7 15

Example

> sum(x2/y2,na.rm=TRUE)

Output

[1] 415.8512

Example

Live Demo

> x3<-sample(c(rnorm(5),0),80,replace=TRUE)
> x3

Output

[1] 0.7030833 0.7030833 2.4352556 0.0000000 -0.7432770 -0.7432770
[7] -0.3303050 -0.2555678 0.0000000 -0.2555678 -0.3303050 0.7030833
[13] -0.7432770 -0.7432770 2.4352556 0.0000000 -0.3303050 -0.3303050
[19] -0.7432770 -0.3303050 -0.7432770 2.4352556 -0.2555678 -0.2555678
[25] -0.3303050 -0.7432770 -0.7432770 -0.7432770 0.7030833 0.0000000
[31] -0.2555678 0.0000000 2.4352556 2.4352556 -0.7432770 -0.7432770
[37] 0.7030833 0.0000000 2.4352556 -0.7432770 -0.3303050 0.0000000
[43] 0.7030833 0.7030833 -0.3303050 2.4352556 -0.2555678 0.7030833
[49] 0.0000000 0.7030833 -0.7432770 0.0000000 0.0000000 2.4352556
[55] 0.0000000 -0.3303050 0.0000000 0.7030833 0.7030833 -0.7432770
[61] 2.4352556 -0.7432770 2.4352556 -0.2555678 0.0000000 0.7030833
[67] 0.7030833 0.0000000 0.0000000 0.0000000 0.0000000 2.4352556
[73] 0.7030833 2.4352556 -0.3303050 2.4352556 -0.3303050 0.7030833
[79] 0.7030833 0.7030833

Example

Live Demo

> y3<-sample(c(rnorm(5),0),80,replace=TRUE)
> y3

Output

[1] 0.0000000 -1.0907582 0.3234917 -0.4812229 0.0000000 0.3234917
[7] -1.0907582 0.5083502 2.3883446 0.5083502 -1.0907582 0.5083502
[13] 0.5083502 0.5083502 -1.0907582 -1.0907582 0.0000000 -0.4812229
[19] 0.3234917 0.5083502 2.3883446 2.3883446 0.5083502 0.5083502
[25] 0.5083502 0.3234917 0.5083502 -0.4812229 -0.4812229 2.3883446
[31] 0.5083502 -0.4812229 0.3234917 0.3234917 0.0000000 2.3883446
[37] -0.4812229 -0.4812229 0.3234917 0.3234917 -1.0907582 2.3883446
[43] -1.0907582 0.3234917 -0.4812229 0.3234917 2.3883446 -0.4812229
[49] -0.4812229 0.0000000 0.0000000 0.5083502 0.3234917 -1.0907582
[55] 0.5083502 2.3883446 0.0000000 0.3234917 0.3234917 -1.0907582
[61] -0.4812229 -0.4812229 0.0000000 0.0000000 0.3234917 -0.4812229
[67] 2.3883446 0.3234917 -1.0907582 0.5083502 -0.4812229 0.3234917
[73] -0.4812229 0.5083502 2.3883446 0.3234917 0.5083502 0.3234917
[79] 0.3234917 2.3883446

Example

> x3[x3==0]<-NA
> y3[y3==0]<-NA
> x3

Output

[1] 0.7030833 0.7030833 2.4352556 NA -0.7432770 -0.7432770
[7] -0.3303050 -0.2555678 NA -0.2555678 -0.3303050 0.7030833
[13] -0.7432770 -0.7432770 2.4352556 NA -0.3303050 -0.3303050
[19] -0.7432770 -0.3303050 -0.7432770 2.4352556 -0.2555678 -0.2555678
[25] -0.3303050 -0.7432770 -0.7432770 -0.7432770 0.7030833 NA
[31] -0.2555678 NA 2.4352556 2.4352556 -0.7432770 -0.7432770
[37] 0.7030833 NA 2.4352556 -0.7432770 -0.3303050 NA
[43] 0.7030833 0.7030833 -0.3303050 2.4352556 -0.2555678 0.7030833
[49] NA 0.7030833 -0.7432770 NA NA 2.4352556
[55] NA -0.3303050 NA 0.7030833 0.7030833 -0.7432770
[61] 2.4352556 -0.7432770 2.4352556 -0.2555678 NA 0.7030833
[67] 0.7030833 NA NA NA NA 2.4352556
[73] 0.7030833 2.4352556 -0.3303050 2.4352556 -0.3303050 0.7030833
[79] 0.7030833 0.7030833

Example

> y3

Output

[1] NA -1.0907582 0.3234917 -0.4812229 NA 0.3234917
[7] -1.0907582 0.5083502 2.3883446 0.5083502 -1.0907582 0.5083502
[13] 0.5083502 0.5083502 -1.0907582 -1.0907582 NA -0.4812229
[19] 0.3234917 0.5083502 2.3883446 2.3883446 0.5083502 0.5083502
[25] 0.5083502 0.3234917 0.5083502 -0.4812229 -0.4812229 2.3883446
[31] 0.5083502 -0.4812229 0.3234917 0.3234917 NA 2.3883446
[37] -0.4812229 -0.4812229 0.3234917 0.3234917 -1.0907582 2.3883446
[43] -1.0907582 0.3234917 -0.4812229 0.3234917 2.3883446 -0.4812229
[49] -0.4812229 NA NA 0.5083502 0.3234917 -1.0907582
[55] 0.5083502 2.3883446 NA 0.3234917 0.3234917 -1.0907582
[61] -0.4812229 -0.4812229 NA NA 0.3234917 -0.4812229
[67] 2.3883446 0.3234917 -1.0907582 0.5083502 -0.4812229 0.3234917
[73] -0.4812229 0.5083502 2.3883446 0.3234917 0.5083502 0.3234917
[79] 0.3234917 2.3883446

Example

> sum(x3/y3,na.rm=TRUE)

Output

[1] 40.23086