df
n = c(2, 3, 5, 8, 10, 12)
s = c("aa", "bb", "cc", "aa", "bb","aa")
b = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
df = data.frame(n, s, b)
I want to calculate the proportion of "s" and then replace the category with "rare" if proportion in "s" is below 20%.
Result:
n s b
1 2 aa TRUE
2 3 bb FALSE
3 5 rare TRUE
4 8 aa FALSE
5 10 bb TRUE
6 12 aa FALSE
I've been able to find how to calculate a proportion but not how to use that proportion to replace a categorical value.
mtcars %>%
count(am, gear) %>%
group_by(am) %>%
mutate(freq = n / sum(n))