Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Integrate fct_other functionality into fct_collapse #100
Thanks for this great package! I'm wondering though whether there would be any possibility of integrating features of
I currently need to recode a factor by collapsing a targeted set of levels (say
This feature would be quite helpful, because the only solution I can think of now is quite cumbersome. That is, I have to manually specify all new (collapsed) factor level names (entered in the
added a commit
Nov 29, 2017
Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!
If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down the page. Please make sure your reprex is created with the reprex package as it gives nicely formatted output and avoids a number of common pitfalls.
+1 for this.
library(forcats) set.seed(123) data <- sample(LETTERS[1:10], 99, replace = TRUE) # What I had to do data %>% fct_collapse(AB = c("A", "B"), CDE = c("C", "D", "E")) %>% fct_other(keep = c("AB", "CDE")) #>  CDE Other CDE Other Other AB Other Other Other CDE Other #>  CDE Other Other AB Other CDE AB CDE Other Other Other #>  Other Other Other Other Other Other CDE AB Other Other Other #>  Other AB CDE Other CDE CDE CDE AB CDE CDE CDE #>  AB AB CDE CDE CDE Other AB CDE Other AB Other #>  CDE AB Other Other CDE Other AB CDE CDE Other CDE #>  Other Other Other CDE Other Other Other AB CDE CDE CDE #>  Other CDE AB CDE Other CDE Other AB CDE Other Other #>  Other AB AB Other CDE Other CDE AB Other AB CDE #> Levels: AB CDE Other # What I like to do # data %>% # fct_collapse(AB = c("A", "B"), CDE = c("C", "D", "E"), group_other = TRUE)