I was just teaching conditional probabilities today! I chose the following method
library(tidyverse)
library(titanic)
titanic_train %>%
filter(
!is.na(Sex),
!is.na(Age)
) %>%
mutate(age_cat = ifelse(Age >= 35, "at least 35", "less than 35")) %>%
count(age_cat, Sex) %>%
group_by(age_cat) %>%
mutate(prop = n / sum(n))
#> # A tibble: 4 x 4
#> # Groups: age_cat [2]
#> age_cat Sex n prop
#> <chr> <chr> <int> <dbl>
#> 1 at least 35 female 81 0.345
#> 2 at least 35 male 154 0.655
#> 3 less than 35 female 180 0.376
#> 4 less than 35 male 299 0.624
However, if you're also introducing tidyr the following is also a good way of going about it:
library(tidyverse)
library(titanic)
titanic_train %>%
filter(
!is.na(Sex),
!is.na(Age)
) %>%
mutate(age_cat = ifelse(Age >= 35, "at least 35", "less than 35")) %>%
count(age_cat, Sex) %>%
spread(Sex, n) %>%
mutate(prop = female / (female + male))
#> # A tibble: 2 x 4
#> age_cat female male prop
#> <chr> <int> <int> <dbl>
#> 1 at least 35 81 154 0.345
#> 2 less than 35 180 299 0.376