Base R and the tidyverse

definetely a big tidyverse fan but I had somehow the same thought watching the General Data science overview seminar. In particular, they make a case for using dplyr for refactoring in cases like this:


mtcars$gear_char <- ifelse(mtcars$gear == 3,
                           "three",
                           ifelse(mtcars$gear == 4,
                                  'four',
                                  'five')
                           )

And they suggest an arguably clearer solution in dplyr:

mtcars$gear_char <- mtcars %>% 
                      mutate(
                        gear_char = (
                          case_when( gear == 3 ~ "three",
                                     gear == 4 ~ "four",
                                     gear == 5 ~ "five")
                        )
                      )

However, I would have found more concise and more informative (in terms of final data type) to use a different base R solution like:

mtcars$gear_char <- ordered(mtcars$gear, labels = c('three', 'four', 'five'))

This is more about having some understanding of base R rather than a solid one, but it made me think of whether sometimes the risk was not to reinvent the wheel.

2 Likes