I think you are looking for sweep() .
> (mat <- matrix(rep(1:3,each=5),nrow=3,ncol=5,byrow=TRUE)) [,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 2 2 2 2 [3,] 3 3 3 3 3 > vec <- 1:5 > sweep(mat,MARGIN=2,vec,`*`) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 2 4 6 8 10 [3,] 3 6 9 12 15
This was one of the core functions of R, although improvements have been made over the years.
hatmatrix Sep 06 '10 at 9:52 2010-09-06 09:52
source share