tidyverse / forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)
https://forcats.tidyverse.org/
Other
554 stars 126 forks source link

Documentation examples to working with pipes in data frames #279

Closed eran3006 closed 2 years ago

eran3006 commented 3 years ago

Hi!

I think many users, probably particular those with less experience in R and the tidyverse like myself, can benefit if the documentation for most of forcats functions would include an example of piping a function to dplyr::mutate when working with data.frames. I think it is a small thing that can save some frustration for people new to forcats.

For example:

df <- tibble(f = factor(c("a", "b", "c", "d"),
                          levels = c("b", "c", "d", "a")))

## this code fails 
df %>% 
  fct_relevel(f, "a") %>% 
  str() 

## this code works as expected. 
df  %>%
  mutate(f = fct_relevel(f, "a")) %>% 
  str() 
hadley commented 2 years ago

This is a good idea, but unfortunately we can't do it because it would require dplyr to be a dependency of forcats, which unfortunately is a bad idea for software engineering reasons.