Chapter 5 Crosstabs

Two cases:

5.1 Case 1: with survey weights

nhanes %>% 
  select(Gender, Education, wt=WTINT2YR) %>% 
  # There's a clever way to do this, but it's easier to write this 3 times.
  filter(!is.na(Gender), !is.na(wt), !is.na(Education)) %>% 
  group_by(Gender, Education) %>% 
  summarise(wt = sum(wt)) %>% 
  group_by(Gender) %>% 
  mutate(wt = scales::percent(wt/sum(wt))) %>% 
  pivot_wider(id_cols = c(Gender, Education), names_from=Gender, values_from=wt) %>% 
  knitr::kable()
## `summarise()` has grouped output by 'Gender'. You can override using the `.groups` argument.
Education female male
8th Grade 5.9% 6.24%
9 - 11th Grade 11.4% 12.14%
High School 20.4% 22.62%
Some College 32.8% 29.73%
College Grad 29.4% 29.28%

5.2 Case 2: Without survey weights

Using the janitor package.

nhanes %>% 
  filter(!is.na(Education), !is.na(Gender)) %>% 
  tabyl(Education, Gender) %>% 
  adorn_percentages("col") %>% 
  adorn_pct_formatting(digits = 2) %>% 
  knitr::kable()
Education female male
8th Grade 11.01% 11.47%
9 - 11th Grade 14.77% 15.65%
High School 20.83% 23.37%
Some College 31.00% 26.72%
College Grad 22.39% 22.80%