Here is input data: a table giving the age classification (agestrat - either a single age year or a range of years) and gender of 20 persons.
library(tidyverse)
df <- tibble(agestrat = c("5", "6", "6", "7", "7", "7", "7", "9", "11", "11", "12", "14", "17", "18", "18", "22", "24", "25-40", "41-50", "51-60"),
             gender = c("male", "female", "female", "male", "male", "male", "female", "female", "male", "female", "female", "male", "female", "female", "male", "male", "female", "male", "male", "female"))
Here is the output I want:
  agestrat  male    female
  5         1   
  6                 2
  7         3       1
  9                 1
  11        1       1
  12                1
  14        1   
  17                1
  18        1       1
  22        1
  24                1
  25-40     1   
  41-50     1   
  51-60             1
A table that aggregates across agestrat and gives the count of males and females within each agestrat. I've tried various approaches in dplyr, including group_by, summarise, and nest, but I'm stumped on how to create the two new columns of male and female and insert the requisite counts within each cell.
Thanks!