rdataframeweighting

How can I weight a column with a number, R?


I have a column with anomaly values and I want to weight it with a specific number representing the number of years (32).

How can I do this?

data(mtcars)
mtcars$weight<-apply(mtcars[5], 1, ??, 32)

Solution

  • mtcars$weighted <- mtcars$drat * 32
    

    Or if your weights are different for each observation

    mtcars$weighted <- mtcars$drat * mtcars$cyl
    

    No need for apply, multiplication is already vectorized for your convenience ;)