Imagine that you have data like this:
set.seed(2021)
age <- floor(runif(35, min = 20, max = 25))
dat <- data.frame(age)
dat %>%
mutate(education = sample(c("Low", "Mid-level", "High"),
size = nrow(dat), prob = c(0.55, 0.2, 0.25), replace = TRUE)) %>%
group_by(age, education) %>%
summarise(n = n())
Results in:
age education n
<dbl> <chr> <int>
1 20 High 1
2 20 Low 2
3 21 Low 3
4 21 Mid-level 2
5 22 High 2
6 22 Low 4
7 23 Low 4
8 23 Mid-level 2
9 24 High 1
10 24 Low 10
11 24 Mid-level 4
As you can see, for example, there are no counts for "Mid-level" education at age 20, so that category is excluded from the data frame. Is it possible to show that value as 0 instead?
E.g.
# A tibble: 11 x 3
# Groups: age [5]
age education n
<dbl> <chr> <int>
1 20 High 1
2 20 Low 2
3 20 Mid-level 0