Create a new variable that is a linear combination of many other variables

Suppose I have a dataframe that looks like this:

df1 <- as.data.frame(matrix( rnorm(100*50,mean=0,sd=1), 100, 50))

      

I want to create a new variable y

that is the sum of alpha_i * V_i with i from 1 to 50 and where alpha is a random number obtained from the uniform distribution (0,1).

What's the best way to do this? Can I do this with mutate

and dplyr

?

+3


source to share


1 answer


You may try

 df1$newvar <- as.matrix(df1) %*% v1

      

or

 df1$newvar <- rowSums(sweep(df1, 2, v1, FUN='*'))

      



Or as suggested by @Frank based on post

 df1$newvar <- Reduce(`+`,lapply(seq_along(v1),function(i)df1[[i]]*v1[i]))

      

data

 set.seed(24)
 df1 <- as.data.frame(matrix( rnorm(100*50,mean=0,sd=1), 100, 50))
 set.seed(48)
 v1 <- runif(50)

      

+3


source







All Articles