I have the following problem :
w <- matrix(1:3,nrow=3,ncol=1)
mymat <- as.matrix(cbind(a = 6:15, b = 16:25, c= 26:35))
mymat
a b c
[1,] 6 16 26
[2,] 7 17 27
[3,] 8 18 28
[4,] 9 19 29
[5,] 10 20 30
[6,] 11 21 31
[7,] 12 22 32
[8,] 13 23 33
[9,] 14 24 34
[10,] 15 25 35
I want to obtain the following results in a matrix the same size as mymat:
a b c
[1,] 6*1 16*2 26*3
[2,] 7*1 17*2 27*3
[3,] 8*1 18*2 28*3
...
I've tried the lappy function but I am unable to get the results I want. Thanks!
Using sweep()
:
sweep(mymat, 2, w, "*")
Converting w
into a matrix of the same dimensions:
mymat * t(w)[rep(1, NROW(mymat)), ]