Community Forums

Find answers, ask questions, and connect with the
GRAPH Courses community.

Home Forums R help Loops Reply To: Loops

  • Kene David

    Administrator
    December 16, 2022 at 6:45 pm

    For this you can use the `.names` argument. Like this:

    df %>% 
    mutate(across(.cols = c("formal_emp_ugx", "personal_emp_ugx", "casual_emp_ugx"),
    .fns = ~ case_when(eval_period == "Baseline" ~ .x/3700,
    eval_period == "Endline" ~ .x/3800,
    eval_period == "Midline" ~ .x/3500),
    .names = "{.col}_usd"
    ))

    That will leave you with names that look like “ugx_usd” though. To fix that you can use the `rename_with()` function which can rename many columns at the same time:

    df %>% 
    mutate(across(.cols = c("formal_emp_ugx", "personal_emp_ugx", "casual_emp_ugx"),
    .fns = ~ case_when(eval_period == "Baseline" ~ .x/3700,
    eval_period == "Endline" ~ .x/3800,
    eval_period == "Midline" ~ .x/3500),
    .names = "{.col}_usd"
    )) %>%
    rename_with(.fn = ~ str_replace_all(.x, "ugx_usd", "usd"))