I have the following data frame with many variables,
> head(fit_dat[,c(1:3)])
var_a var_b var_c
1 1.14 2.3815 1.0606
2 0.83 1.5818 1.2450
3 0.92 1.8848 1.0606
4 0.96 1.4596 1.0606
5 1.16 0.9677 1.0248
6 0.81 2.4058 1.1189
I also have a vector with elements that correspond to each of the variables in my data frame by name
> g[c(1:3)]
var_a
1.4020096
var_b
0.9118361
var_c
1.2868801
I want to mutate every column of my data frame without naming all of the many columns that it has, and I want to do this dynamically such that the variables names are used inside the ~function. I attempt to do this with the following but it doesnt work. How could I accomplish this without using joins, loops or naming every variable?
And more generally, I've been wondering, if I insert a such function in mutate_all, what is passed to that function in any one computation ?
library(tidyverse)
fit_dat %>% mutate_all(list(z = ~ . * g[colnames(.)])) # this `colnames` call is the problem!
Thank you!