Skip to content

Instantly share code, notes, and snippets.

@ckholmes5
Created August 4, 2016 15:57
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/dff0d2d211262723523a09c2e05aeffd to your computer and use it in GitHub Desktop.
Save ckholmes5/dff0d2d211262723523a09c2e05aeffd to your computer and use it in GitHub Desktop.
clean = select(df, YEAR, STATE, FATALS, A_DIST, A_SPCRA, A_POSBAC)
clean$YEAR = as.character(clean$YEAR)
#All Fatalities by year
total_fatal = summarise(group_by(clean, YEAR), FATALS = sum(FATALS))
#Drunk Driving
dd = mutate(clean, A_POSBAC=replace(A_POSBAC, A_POSBAC>1, 0))
dd = mutate(dd, drunk_fatals = FATALS*A_POSBAC)
dd_fatal = summarise(group_by(dd, YEAR), FATALS = sum(drunk_fatals))
#Distracted Driving
distract = mutate(clean, A_DIST=replace(A_DIST, A_DIST>1, 0))
distract = mutate(distract, distract_fatals = FATALS*A_DIST)
distract_fatal = summarise(group_by(distract, YEAR), FATALS = sum(distract_fatals))
#Speeding
speeding = mutate(clean, A_SPCRA = replace(A_SPCRA, A_SPCRA > 1, 0))
speeding = mutate(speeding, speeding_fatals = A_SPCRA*FATALS)
speeding_fatal = summarise(group_by(speeding, YEAR), FATALS = sum(speeding_fatals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment