Consider the following table:
df <- tibble(
col1 = sample(10:1000, 5),
col2 = sample(1000:10000, 5),
col3 = sample(100:500, 5)
)
I'd like to divide col1 and col3 by 10, and col2 by 1000.
I can do this by using 2 mutate_at functions like so:
df %>%
mutate_at(
c("col1", "col3"),
~ .x / 10
) %>%
mutate_at(
"col2",
~ .x / 1000
)
Is there a way to do the above in a single mutate_x function?
dplyr, you could useacrossand do this with onemutate.df %>% mutate(across(col1, col2), ~.x/10, across(col3), ~.x/1000). See here tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise