How to multiply each element of a numerical vector in R?

R ProgrammingServer Side ProgrammingProgramming

Sometimes we want to determine the multiplication of all the elements of a vector just like the sum. This might be required to test the changes in the mathematical operations that can be applied to a numerical vector. In base R, we have prod function which works same as sum but give us the multiplication of all the elements of a vector.

Example

 Live Demo

> v1<-1:5
> v1

Output

[1] 1 2 3 4 5
> prod(v1)

Output

[1] 120

Example

 Live Demo

> v2<-rnorm(10)
> v2

Output

[1] -0.500466629 0.394771317 0.575743107 0.026982141 0.812697502
[6] 0.995708241 2.198243938 -0.008609976 -0.931337300 -0.073743225
> prod(v2)

Output

[1] 3.228448e-06

Example

 Live Demo

> v3<-rnorm(10,1.5)
> v3

Output

[1] 1.7328245888 -0.5772304935 2.5161349689 3.0401656274 0.1669773313
[6] -0.0001252235 0.7649984733 2.4901543043 1.5618729422 2.4392364199
> prod(v3)

Output

[1] 0.001161087

Example

 Live Demo

> v4<-rpois(10,2)
> v4

Output

[1] 3 2 3 5 0 0 3 2 0 4
> prod(v4)

Output

[1] 0

Example

 Live Demo

> v5<-rpois(100,20)
> v5

Output

[1] 17 31 19 15 20 25 18 22 18 29 22 19 20 26 21 23 14 34 14 14 19 15 24 23 20
[26] 14 23 26 14 17 15 32 21 17 16 23 19 24 22 15 27 19 20 26 11 18 26 20 23 16
[51] 26 17 16 15 12 20 25 18 15 18 22 22 20 22 19 14 13 18 23 24 23 21 21 23 23
[76] 13 18 24 16 21 17 17 18 20 29 18 12 27 16 23 26 19 28 21 14 19 25 18 22 17
> prod(v5)

Output

[1] 1.764217e+129

Example

 Live Demo

> v6<-runif(50,2,5)
> v6

Output

[1] 4.384473 2.464259 3.052674 4.754015 3.149084 2.391700 3.156066 4.546584
[9] 3.473825 3.438115 2.668482 3.956325 2.315420 2.799798 3.830397 4.223601
[17] 4.617783 3.141474 4.908374 2.274919 3.714314 2.893030 4.800681 2.211926
[25] 4.778749 3.508617 3.073959 2.819000 4.860615 3.846824 2.608646 2.810488
[33] 2.562088 2.409402 3.357812 2.668864 4.677772 2.392214 2.070477 3.510179
[41] 3.333990 3.905242 4.144810 4.614480 2.137121 3.329712 3.224403 4.085072
[49] 2.697773 4.842452
> prod(v6)

Output

[1] 1.131786e+26

Example

 Live Demo

> v7<-rexp(50,2)
> v7

Output

[1] 0.196256425 0.778689736 0.422888133 0.508051269 0.002485082 0.255892951
[7] 0.138419567 0.130393627 0.846025451 0.283145254 2.114919795 0.263044225
[13] 0.174996635 0.141883192 0.099179770 0.039533379 0.164351775 0.027566355
[19] 0.540246424 0.775166984 0.060083801 0.639466961 0.115555783 0.328276267
[25] 0.025136740 1.223544536 0.080570191 0.350975182 1.143206199 0.649686242
[31] 0.481954727 0.171125844 0.583099992 0.190488726 0.025616325 0.340935745
[37] 0.060853943 1.541687444 0.544305771 0.001036567 0.037164253 0.247312260
[43] 1.175060552 0.212010939 0.104288578 0.072779226 0.343334238 0.305414554
[49] 1.682843018 0.751852211
> prod(v7)

Output

[1] 8.528215e-35

Example

 Live Demo

> v8<-sample(c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9),50,replace=TRUE)
> v8

Output

[1] 0.5 0.8 0.5 0.3 0.3 0.1 0.5 0.6 0.7 0.9 0.3 0.2 0.3 0.4 0.5 0.2 0.5 0.1 0.9
[20] 0.3 0.5 0.6 0.8 0.2 0.8 0.9 0.2 0.3 0.6 0.3 0.7 0.7 0.1 0.9 0.4 0.6 0.5 0.2
[39] 0.3 0.6 0.1 0.2 0.1 0.6 0.1 0.8 0.1 0.4 0.8 0.4
> prod(v8)

Output

[1] 2.88936e-22

Example

 Live Demo

> v9<-sample(1:5,50,replace=TRUE)
> v9

Output

[1] 4 3 1 2 3 1 5 3 1 5 4 2 3 5 2 2 4 5 4 5 5 3 1 3 2 2 5 1 1 1 1 5 1 5 2 1 5 1
[39] 5 5 4 1 3 1 1 1 5 3 2 4
> prod(v9)

Output

[1] 8.39808e+18

Example

 Live Demo

> v10<-sample(-5:5,50,replace=TRUE)
> v10

Output

[1] 1 4 -1 -1 2 -3 0 -2 0 -4 2 4 0 2 -3 -1 1 2 5 -2 -3 -2 3 2 1
[26] -2 -1 2 5 4 0 -3 1 -4 -4 -2 2 2 -3 -3 -5 2 1 0 -4 -3 -3 -3 4 2
> prod(v10)
[1] 0
raja
Published on 07-Sep-2020 06:10:18
Advertisements