Skip to content

Instantly share code, notes, and snippets.

@ckholmes5
Created August 4, 2016 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckholmes5/88e87bc8c64bbf7696634ef57ae8bece to your computer and use it in GitHub Desktop.
Save ckholmes5/88e87bc8c64bbf7696634ef57ae8bece to your computer and use it in GitHub Desktop.
state_data = merge(x=clean, y=statepop, by.x=c('YEAR', 'STATE'), by.y=c('Year', 'State'))
state_data = mutate(state_data, A_SPCRA = replace(A_SPCRA, A_SPCRA > 1, 0), A_DIST=replace(A_DIST, A_DIST>1, 0), A_POSBAC=replace(A_POSBAC, A_POSBAC>1, 0))
state_data = mutate(state_data, drunk_fatals = FATALS*A_POSBAC, distract_fatals = FATALS*A_DIST,speeding_fatals = A_SPCRA*FATALS)
grouped_state = summarise(group_by(state_data, YEAR, STATE, Code), total_fatals = sum(FATALS), drunk_fatals = sum(drunk_fatals), distract_fatals = sum(distract_fatals), speeding_fatals = sum(speeding_fatals), Population = mean(Population))
grouped_state = mutate(grouped_state, total_rate = total_fatals*100000/Population, drunk_rate = drunk_fatals*100000/Population, distract_rate = distract_fatals*100000/Population, speeding_rate = speeding_fatals*100000/Population)
mapdat = grouped_state
mapdat = mapdat[mapdat$YEAR >= 2001 & mapdat$YEAR <= 2007,]
mapdat = summarise(group_by(mapdat, Code), rate = mean(total_rate))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment