How to add a vector to each row of a matrix in R?

R ProgrammingServer Side ProgrammingProgramming

To add a vector to reach row of a matrix, we can use addition sign (+) and create the repetition of the vector up to the number of rows in the matrix. For example, if we have a matrix called M then a vector say v can be added to each row of M by using the command −

M+rep(v,each=nrow(M))

Example1

Consider the below matrix and the vector −

Live Demo

> M1<-matrix(rpois(40,2),nrow=20)
> M1

Output

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

Adding v1 to rows in M1 −

> M1+rep(v1,each=nrow(M1))

Output

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

Example2

Live Demo

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

Output

             [,1]        [,2]        [,3]
 [1,] -1.16459899  0.04276452 -0.38747561
 [2,]  0.50198231  1.40818681  1.34047754
 [3,] -0.83571273  0.15311835 -0.66026732
 [4,] -1.04751005  0.68401101  0.36614494
 [5,] -0.13906013 -0.08104307  1.58938567
 [6,]  0.79923477  0.13871823  1.19483957
 [7,] -0.70957734 -1.22610985  0.79431236
 [8,] -0.61919335 -1.67900016  0.75673298
 [9,]  0.02131366  0.59198453 -0.51860397
[10,] -0.92114971 -0.94043054 -0.66674705
[11,] -0.26933585  0.61537773  1.18988144
[12,]  2.11994998 -0.62014441 -0.97012363
[13,] -0.45798423  0.92096389  0.74603167
[14,] -0.51599135 -0.01450992 -1.84365984
[15,] -0.29866554  0.99900886 -0.55598877
[16,] -0.91226758 -1.63915166 -0.20200339
[17,]  0.40107684  1.79162856 -0.02759807
[18,]  0.44712300 -0.07322323 -0.15221520
[19,]  0.15838286 -1.50611267 -0.07117655
[20,]  0.53166819 -0.99748658 -0.54070065
> v2<-c(100,100,100)

Adding v2 to rows in M2 −

> M2+rep(v2,each=nrow(M2))

Output

           [,1]      [,2]      [,3]
 [1,]  98.83540 100.04276  99.61252
 [2,] 100.50198 101.40819 101.34048
 [3,]  99.16429 100.15312  99.33973
 [4,]  98.95249 100.68401 100.36614
 [5,]  99.86094  99.91896 101.58939
 [6,] 100.79923 100.13872 101.19484
 [7,]  99.29042  98.77389 100.79431
 [8,]  99.38081  98.32100 100.75673
 [9,] 100.02131 100.59198  99.48140
[10,]  99.07885  99.05957  99.33325
[11,]  99.73066 100.61538 101.18988
[12,] 102.11995  99.37986  99.02988
[13,]  99.54202 100.92096 100.74603
[14,]  99.48401  99.98549  98.15634
[15,]  99.70133 100.99901  99.44401
[16,]  99.08773  98.36085  99.79800
[17,] 100.40108 101.79163  99.97240
[18,] 100.44712  99.92678  99.84778
[19,] 100.15838  98.49389  99.92882
[20,] 100.53167  99.00251  99.45930
raja
Published on 05-Mar-2021 07:11:10
Advertisements