DS4PS / course_website

https://ds4ps.github.io/course_website/
0 stars 0 forks source link

Dividing total injuries by total incidents #12

Open swkilar opened 5 years ago

swkilar commented 5 years ago

I am struggling with Question 2 of Lab 9. I have an answer of 30.2 percent of the crashes over the past few years have resulted in an injury or a fatality, but I did not get that answer fully using dplyr functions. I just used a calculator. I was able to get the total number of incidents, and the total number of incidents with injuries or fatalities, but now I cannot figure out how to divide those two for a percentage. Any ideas? Here's my code so far:

totalcrasheswith.injuriesorfatalities <-
dat %>%
  count( (Totalinjuries > 0 | Totalfatalities > 0 ) )

number.of.incidents <-
dat %>%
  group_by(Incidentid) %>%
  summarise( count=n())

dat %>%
 WHAT'S THE FUNCTION HERE( AND HERE = totalcrasheswith.injuriesorfatalities / number.of.incidents )

Thanks, all.

lecy commented 5 years ago

You are overthinking it:

these.crashes <- injuries > 0 | fatalities > 0
mean( these.crashes )

(pseudocode above)

In dplyr you can use summarize() and sum of these.crashes divided by n().