Is there a way to modify each value in a data frame variable by mutating each value by the mean of the variable? I'm trying to do use mutate_all and hitting a wall on my understanding. I've tried a few approaches and nothing seems to work.
I want to do something like this:
sw_height_mass <- starwars %>% select(height, mass) starwars_means <- starwars %>% select(height, mass) %>% mutate_all(mean, na.rm = TRUE) %>% rename(hmean = height, mmean = mass) starwars_what_i_want <- bind_cols(sw_height_mass, starwars_means) %>% mutate(new_height = height / hmean, new_mass = mass / mmean) %>% select(5:6) starwars_what_i_want
Ideally with less code, I imagine something like this...
starwars %>% select(height, mass) %>% mutate_all((. / mean), na.rm = TRUE)
But my imagination doesn't match reality and I'm not figuring out what will work. My actual data has a lot more variables.
Thanks for any consideration.