I'm not sure how I should phrase this.
Let's say I have a data frame about juvenile arrests. One of the columns describes the statutes broken, and that's what I'm doing a group_by() for.
date sex statute_description
2015-01-01 M ROBBERY
2015-01-01 M ROBBERY
2015-01-01 F ROBBERY
2015-01-01 F ASSAULT - SIMPLE
2015-01-01 F ASSAULT - SIMPLE
2015-01-01 M ASSAULT - SIMPLE
2015-01-01 M DRUG POSSESSION
2015-01-01 M ISSUANCE OF WARRANT
2015-01-01 M ISSUANCE OF WARRANT
2015-01-01 M ISSUANCE OF WARRANT
arrest_reasons <- group_by(df, statute_description) %>%
summarize(
num_arrests = n
)
This would return:
statute_description num_arrests
ROBBERY 3
DRUG POSSESSION 1
ASSAULT - SIMPLE 3
ISSUANCE OF WARRANT 3
What I want to do is add another column to the data frame, that counts how many members of each sex committed a particular crime. Like this:
statute_description num_arrests males females
ROBBERY 3 2 1
DRUG POSSESSION 1 1 0
ASSAULT - SIMPLE 3 1 2
ISSUANCE OF WARRANT 3 3 0
I'm not sure what function would be suitable for this.