How to divide columns of a matrix by vector elements in R?


Suppose we have a vector say V that contains five elements and a matrix say M that has five columns. Now again, suppose that we want to divide each column in M by corresponding value in vector V, which means first column in M will be divided by first value in the V and so on then we can use the sweep function as shown below −

sweep(M,2,V,FUN="/")

Example 1

Consider the below matrix and vector −

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

The following dataframe is created

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

To divide columns of M1 with corresponding values in V1 on the above created data frame, add the following code to the above snippet −

M1<-matrix(rpois(80,10),ncol=4)
V1<-1:4
sweep(M1,2,V1,FUN="/")

Output

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

     [,1]  [,2]      [,3]  [,4]
[1,]   9   7.5  3.333333  3.00
[2,]   9   5.5  3.333333  2.50
[3,]   9   5.5  3.666667  1.00
[4,]  10   6.5  3.666667  2.50
[5,]   9   6.0  2.333333  1.75
[6,]   8   4.5  3.000000  4.00
[7,]   7   4.5  4.000000  2.00
[8,]  12   3.5  4.333333  2.75
[9,]  16   4.0  3.333333  1.50
[10,] 12   5.0  2.333333  2.25
[11,] 14   4.5  4.000000  2.50
[12,] 10   4.0  1.666667  2.75
[13,]  8   6.5  2.666667  2.50
[14,]  8   2.5  2.333333  2.00
[15,] 10   7.0  2.666667  3.25
[16,]  7   5.0  3.000000  1.75
[17,] 11   4.5  3.333333  3.00
[18,] 15   5.5  5.666667  2.00
[19,]  6   8.0  4.666667  3.50
[20,] 12   3.0  4.333333  2.00

Example 2

Consider the matrix given below −

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

The following dataframe is created

        [,1] [,2]
[1,]   0.91 -0.03
[2,]  -0.49 -0.23
[3,]  -0.26  0.10
[4,]  -0.05 -1.16
[5,]   0.86 -2.16
[6,]   0.58 -0.62
[7,]   0.86 -0.67
[8,]   0.32 -0.54
[9,]   0.90 -0.05
[10,]  0.72 -0.88
[11,] -0.46 -1.61
[12,]  0.02 -0.39
[13,]  1.09 -0.09
[14,] -0.67  0.64
[15,] -1.85 -1.31
[16,] -3.32 -1.12
[17,]  1.39  0.08
[18,] -1.25 -0.55
[19,]  0.50  0.93
[20,]  1.02 -1.29

To divide the columns of M2 with corresponding values in V2 on the above created data frame, add the following code to the above snippet −

M2<-matrix(round(rnorm(40),2),ncol=2)
V2<-c(0.5,0.10)
sweep(M2,2,V2,FUN="/")

Output

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

        [,1]    [,2]
[1,]   1.82    -0.3
[2,]  -0.98    -2.3
[3,]  -0.52     1.0
[4,]  -0.10   -11.6
[5,]   1.72   -21.6
[6,]   1.16    -6.2
[7,]   1.72    -6.7
[8,]   0.64    -5.4
[9,]   1.80    -0.5
[10,]  1.44    -8.8
[11,] -0.92   -16.1
[12,]  0.04    -3.9
[13,]  2.18    -0.9
[14,] -1.34     6.4
[15,] -3.70   -13.1
[16,] -6.64   -11.2
[17,]  2.78     0.8
[18,] -2.50    -5.5
[19,]  1.00     9.3
[20,]  2.04   -12.9

Updated on: 28-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements