Suppose I want to create a mean variable in a given dataframe based on two vectors, one specifying the names of the variables to use, and one specifying weights by which these variables should go into the mean variable:
vars <- c("a", "b", "c","d"))
weights <- c(0.5, 0.7, 0.8, 0.2))
df <- data.frame(cbind(c(1,4,5,7), c(2,3,7,5), c(1,1,2,3),
c(4,5,3,3), c(3,2,2,1), c(5,5,7,1)))
colnames(df) <- c("a","b","c","d","e","f")
How could I use dplyr::mutate()
to create a mean variable that uses vars
and weights
to calculate a rowwise score? mutate()
should specifically use the variables supplied by vars
The result should basically do the following:
df <- df %>%
rowwise() %>%
mutate(comp = mean(c(vars[1]*weights[1], vars[2]*weights[2], ...)))
Written out:
df2 <- df %>%
rowwise() %>%
mutate(comp = mean(c(0.5*a, 0.7*b, 0.8*c, 0.2*d)))
I can't figure out how to do this because, although vars
contains the exact variable names that I want to use for mutate in my df
, inside vars
they are strings. How could I make mutate()
understand that the strings vars
contains relate to columns in my df
? If you know another procedure not using mutate()
that's fine also. Thanks!