r - multiply each cell of a data.frame with it's weight -
what want embarrassing simple - nevertheless fail.
i have data.frame "characters" , "numerics". 1 of columns of data.frame represents weights.
i want multiply every cell of data frame corresponding weight (if it's numeric).
how do (best without using nested loop).
thank in advance!
example:
c1 c2 w l1 abc 2 1 l2 dxf 3 0.5 l3 ghi 4 1.5
should become
c1 c2 w l1 abc 2 1 l2 dxf 1.5 0.5 l3 ghi 6 1.5
for reproducible example, dd
data frame mixture of variable types, w
being weights.
dd <- data.frame(g=gl(2,2), x=rnorm(4), y=1l:4l, z=letters[1:4], w=0.3:3.3) num.vars <- names(dd)[sapply(dd, is.numeric)] #select numeric variables num.vars <- setdiff(num.vars, "w") # remove weight variable dd[num.vars] <- dd[num.vars] * dd$w # multiply
Comments
Post a Comment