R: How to multiply list elements by a vector? -
i want multiply list of matrices (with 1 row), this:
lst <- list("111.2012"=matrix(c(1, 0, 6, na, 1, 0), nrow = 1, byrow = t), "112.2012"=matrix(c(6, 2, 2, 0, 3, na), nrow = 1, byrow = t))
with vector (with same length each matrix):
vec <- c(1,2,3,1,2,3)
and expect result:
$`111.2012` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 0 18 na 2 0 $`112.2012` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 6 4 6 0 6 na
i tried far this:
mapply("*", lst, vec) map("*", lst, vec)
which gives 3 times more numbers , wrong ones. thought of using lapply within mapply adress list, didn't know how it. suggestions? thanks
i believe looking lapply()
, since using list lapply( lst, fun= function(x) x*vec)
for more information see this.
hope helps!
lapply(lst,fun= function(x) x*vec) ## old clunky way lapply(lst, "*" , vec) ## informed richard scriven (thanks!) $`111.2012` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 0 18 na 2 0 $`112.2012` [,1] [,2] [,3] [,4] [,5] [,6] [1,] 6 4 6 0 6 na
Comments
Post a Comment