Is it possible to use dynamic variables on the RHS of dplyr::mutate()?
Toy example:
temp <- tibble(
bl = c(1,2,3,4,5),
fu = c(11,22,33,44,55)
)
bl_var = "bl"
replacement_var = "fu"
# I want a dynamic version of this:
temp %>%
mutate(bl = fu)
# Something like:
temp %>%
mutate(!!bl := !!fu)
In my real use case I have a big set of variables measured at baseline, and another set of variables measured at some follow-up period. I don't want to pivot the tibble longer, but want to replace the baseline variables with the follow-up variables.
The variable have a consistent naming scheme:
baseline variables might be: x_1, x_2, x_3, ...
follow-up variable would then be: x_fu_1, x_fu_2, x_fu_3, ...
Thanks to @akrun for the tidy solution. For anyone else with similar scenario, I modified their solution to join updated baseline variables back to original data:
map2_dfc(baseline, followup, ~ temp %>%
group_by_at(vars(!! row_id)) %>%
transmute(!! .y := !! rlang::sym(.x))) %>%
right_join(temp, by = row_id)