I would like a way to collapse levels of a factor based on the number of observations for each level.
For example, if I have the tibble below with a factor column of animals (four levels: cat, dog, hamster, goldfish), can I collapse levels with less than 2 observations into a level called "other"?
# A tibble: 7 × 1
animal
<fct>
1 cat
2 cat
3 cat
4 dog
5 dog
6 hamster
7 goldfish
This should result in the following...
# A tibble: 7 × 2
animal animal2
<fct> <fct>
1 cat cat
2 cat cat
3 cat cat
4 dog dog
5 dog dog
6 hamster other
7 goldfish other
I would like to be able to adjust the cut-off (e.g. groups with less that 5 observations) and ideally this would be done using tidyverse.