lost
lost

Reputation: 1669

passing a vector into case_when inside mutate_at

I want to create three new variables (call them one, two, and three) using the same generalized mutate but with a different existing variable used inside the mutate. To do this I want to write a short code block which accomplishes the same thing as the following (verbose) code:

mtcars.modified <- mtcars %>%

  mutate(one = factor(case_when(
    mpg < 10 ~ "lt10",
    mpg >= 10 & mpg <= 20 ~ "10to20",
    mpg > 20 ~ "gt20"),
    ordered=T, levels = c("lt10", "10to20", "gt20"))) %>%

  mutate(two = factor(case_when(
    disp < 10 ~ "lt10",
    disp >= 10 & disp <= 20 ~ "10to20",
    disp > 20 ~ "gt20"),
    ordered=T, levels = c("lt10", "10to20", "gt20"))) %>%

  mutate(three = factor(case_when(
    qsec < 10 ~ "lt10",
    qsec >= 10 & qsec <= 20 ~ "10to20",
    qsec > 20 ~ "gt20"),
    ordered =T, levels = c("lt10", "10to20", "gt20")))

One way I can generalize this is by using mutate_at's suffixing behavior, and then renaming afterward:

mtcars.modified <- mtcars %>%
  mutate_at(c("mpg", "disp", "qsec"),
            funs(mod = factor(case_when(
              . < 10 ~ "lt10",
              . >= 10 & . <= 20 ~ "10to20",
              . > 20 ~ "gt20"),
              ordered =T, levels = c("lt10", "10to20", "gt20")))) %>%
  rename(one = mpg_mod,
         two = disp_mod,
         three = qsec_mod)

This feels like a workaround, though. Is there a way I can do this without needing to rename afterward? I wondered if I could give one, two, and three as the .vars and then somehow pass the second set of variables into the case_when. It feels analogous to a map2 problem where you have two corresponding vectors and a function which takes items from both vectors in pairs.

This was my (failed) attempt to try to use map2 inside the funs argument:

mtcars.modified <- mtcars %>%
  mutate_at(c("one", "two", "three"),
            funs(map2(.x = ., .y = c(mpg, disp, qsec), 
                      ~ factor(case_when(
                        .y < 10 ~ "lt10",
                        .y >= 10 & .y <= 20 ~ "10to20",
                        .y > 20 ~ "gt20"),
                        ordered =T, levels = c("lt10", "10to20", "gt20")))))

I'd like to keep everything inside a mtcars %>% pipe without creating a named function or breaking the pipe.

Upvotes: 1

Views: 1398

Answers (2)

Eric
Eric

Reputation: 3473

If you use the dplyr::vars function you can rename before applying your function.

mtcars %>%
  mutate_at(
    vars(one = mpg, two = disp, three = qsec),
    funs(
      case_when(
        . < 10 ~ 'lt10',
        . >= 10 & . <= 20 ~ "10to20",
        . > 20 ~ 'gt20'
      ) %>%
        ordered(levels = c('lt10', '10to20', 'gt20'))
    )
  )

This also works with @seisdrum's great suggestion to use base::cut

mtcars %>%
  mutate_at(
    vars(one = mpg, two = disp, three = qsec),
    cut,
    breaks = c(-Inf, 10, 20, Inf),
    labels = c("lt10", "10to20", "gt20")
  )

Upvotes: 1

Clemens Hug
Clemens Hug

Reputation: 497

library(tidyverse)
mtcars %>%
  dplyr::mutate_at(c("mpg", "disp", "qsec"), cut,
                   breaks = c(-Inf, 10, 20, Inf),
                   labels = c("lt10", "10to20", "gt20")) %>%
  head()
#>      mpg cyl disp  hp drat    wt   qsec vs am gear carb
#> 1   gt20   6 gt20 110 3.90 2.620 10to20  0  1    4    4
#> 2   gt20   6 gt20 110 3.90 2.875 10to20  0  1    4    4
#> 3   gt20   4 gt20  93 3.85 2.320 10to20  1  1    4    1
#> 4   gt20   6 gt20 110 3.08 3.215 10to20  1  0    3    1
#> 5 10to20   8 gt20 175 3.15 3.440 10to20  0  0    3    2
#> 6 10to20   6 gt20 105 2.76 3.460   gt20  1  0    3    1

You could use the cut function for this task. Does this do what you want?

If you want to keep the original columns and need the suffix _mod in the modifed ones you can do this:

library(tidyverse)
mtcars %>%
  dplyr::mutate_at(c("mpg", "disp", "qsec"),
                   dplyr::funs(
                     mod = cut(.,
                               breaks = c(-Inf, 10, 20, Inf),
                               labels = c("lt10", "10to20", "gt20")
                               )
                     )
                   ) %>%
  head()
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb mpg_mod disp_mod
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4    gt20     gt20
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4    gt20     gt20
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1    gt20     gt20
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1    gt20     gt20
#> 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2  10to20     gt20
#> 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1  10to20     gt20
#>   qsec_mod
#> 1   10to20
#> 2   10to20
#> 3   10to20
#> 4   10to20
#> 5   10to20
#> 6     gt20

Upvotes: 1

Related Questions