How to find the row products for each row in an R matrix?

R ProgrammingServer Side ProgrammingProgramming

To find the row products for each row in an R matrix, we can use rowProds function of matrixStats package.

For Example, if we have a matrix called MATRIX then we can find the row products for each row in MATRIX by using the command given below −

rowProds(MATRIX)

Example 1

Following snippet creates a sample matrix −

M1<-matrix(rpois(80,10),ncol=4)
M1

The following matrix is created −

     [,1] [,2] [,3] [,4]
[1,]  10   16   12   20
[2,]  16   17    7    9
[3,]   3    9   10    5
[4,]  12    3   13   18
[5,]  16    6    8   10
[6,]  11   10   15   10
[7,]  12   11   13   12
[8,]   5   10   11   11
[9,]   5   12   11    5
[10,] 10   13    7   11
[11,] 10   18   11    4
[12,] 10   11   16   17
[13,]  8    7    9   10
[14,] 10    5    8    7
[15,] 12   11    8    8
[16,] 13   14    7   10
[17,]  9    7    8   11
[18,]  8    6    9    4
[19,]  6    7   14    6
[20,] 10    8    8   15

To load matrixStats package and find row products for each row in M1 on the above created matrix, add the following code to the above snippet −

M1<-matrix(rpois(80,10),ncol=4)
library(matrixStats)
rowProds(M1)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 [1] 38400 17136 1350 8424  7680 16500 20592 6050 3300 10010 7920 29920
[13] 5040  2800  8448 12740 5544 1728  3528  9600

Example 2

Following snippet creates a sample matrix −

M2<-matrix(round(rnorm(60),2),ncol=3)
M2

The following matrix is created −

       [,1] [,2]  [,3]
 [1,] -0.15  0.54 -0.63
 [2,]  0.37 -0.56  0.04
 [3,]  0.97 -0.98  0.08
 [4,] -1.84  0.91  1.51
 [5,] -0.35 -0.75  1.20
 [6,] -0.09 -1.09  0.76
 [7,]  0.23  1.65  0.08
 [8,] -0.25  0.83  0.03
 [9,] -1.05 -1.19 -1.34
[10,] -0.87  1.21 -0.18
[11,] -0.64 -0.83  1.11
[12,] -0.13  1.98  0.33
[13,]  1.13 -0.02 -0.23
[14,]  0.00  0.98 -1.16
[15,]  1.57 -0.73  0.45
[16,]  0.13  1.38 -0.96
[17,] -0.35 -0.22  0.04
[18,]  0.03 -0.67 -0.75
[19,]  0.77  0.07  0.24
[20,]  0.64 -0.27 -0.28

To find row products for each row in M2 on the above created data frame, add the following code to the above snippet −

M2<-matrix(round(rnorm(60),2),ncol=3)
rowProds(M2)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1]   0.051030 -0.008288 -0.076048 -2.528344  0.315000 0.074556 0.030360
[8]  -0.006225 -1.674330  0.189486  0.589632 -0.084942 0.005198 0.000000
[15] -0.515745 -0.172224  0.003080  0.015075  0.012936 0.048384

Example 3

Following snippet creates a sample matrix −

M3<-matrix(round(runif(40,1,5),4),ncol=2)
M3

The following matrix is created −

         [,1] [,2]
 [1,] 3.8294 3.5931
 [2,] 3.2474 2.3081
 [3,] 3.0098 1.2742
 [4,] 4.8341 3.1084
 [5,] 3.8252 4.3888
 [6,] 1.3127 3.7109
 [7,] 2.2684 3.6221
 [8,] 4.3451 3.1057
 [9,] 1.5380 1.5950
[10,] 2.3099 1.4975
[11,] 1.5884 4.5375
[12,] 3.6575 1.3086
[13,] 4.5151 2.0521
[14,] 3.6870 1.1381
[15,] 4.8643 1.5867
[16,] 1.9543 3.4390
[17,] 1.9633 3.9299
[18,] 1.0350 1.8435
[19,] 3.6102 4.6444
[20,] 1.6571 2.5614

To find row products for each row in M3 on the above created data frame, add the following code to the above snippet −

M3<-matrix(round(runif(40,1,5),4),ncol=2)
rowProds(M3)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 [1] 13.759417 7.495324 3.835087 15.026316 16.788038 4.871298 8.216372
 [8] 13.494577 2.453110 3.459075 7.207365  4.786205  9.265437 4.196175
[15] 7.718185  6.720838 7.715573 1.908022  16.767213 4.244496
raja
Updated on 10-Nov-2021 08:16:49

Advertisements