0
votes
mpg |>
  dplyr::mutate_if(is.character, as.factor) |>
  dplyr::mutate_if(is.logical, as.factor) |>
  dplyr::mutate_if(is.integer, as.numeric)

I have the above code. Is there a more simple way to write this code without calling mutate_if three times?

1
did you try dplyr::case_when? - Eric

1 Answers

2
votes

If you check the documentation of mutate_if, it's been superseded by across().

So the above code could be written as:

library(dplyr)

mpg %>% 
  mutate(across(where(is.character) | where(is.logical), as.factor),
         across(where(is.integer), as.numeric))