3

Suppose I have a data frame 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 alpha_i*V_i with i from 1 to 50 and where alpha is a random number drawn from a uniform distribution (0,1).

What is the best way of doing this? Can I do it with mutate and dplyr?

josliber
  • 43,891
  • 12
  • 98
  • 133
Ignacio
  • 7,646
  • 16
  • 60
  • 113

1 Answers1

3

You can try

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

Or

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

Or as suggested by @Frank based on the 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)

 
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    `sweep` also coerces to `matrix`, I guess. eddi suggested `Reduce(\`+\`,lapply(seq_along(v1),function(i)df1[[i]]*v1[i]))` when I asked almost the same question: http://stackoverflow.com/a/19279500/1191259 – Frank Jun 05 '15 at 18:59
  • @Frank Thanks, in this case the `sweep` output is a data.frame, but I guess `lapply` would be fast for big datasets. – akrun Jun 05 '15 at 19:01