Lumping variables by proportion
Many times, you won't have specific levels you want to change to other or collapse together. Rather, you want to keep the most common levels and put everything else into "other." Especially when there are many levels and most of them rare, this is helpful for displaying your data. Let's try this out using the question from the Kaggle survey about which machine learning methods people wanted to try next year. multiple_choice_responses
has been loaded for you. When you're counting, remember that sort = TRUE
corresponds to descending order by default.
This exercise is part of the course
Categorical Data in the Tidyverse
Exercise instructions
- Remove people who didn't select a method.
- Create a new variable,
ml_method
, fromMLMethodNextYearSelect
that preserves titles that at least 5% of respondents have and lump the rest as "Other" (the default value). - Finally, count your new variable, sorted in descending order.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
multiple_choice_responses %>%
# Remove NAs of MLMethodNextYearSelect
filter(___) %>%
# Create ml_method, which lumps all those with less than 5% of people into "Other"
mutate(ml_method = ___(MLMethodNextYearSelect, ___)) %>%
# Count the frequency of your new variable, sorted in descending order
___(___, ___)