cssearcy / AYS-R-Coding-SPR-2020

Coding in R for Policy Analytics
https://cssearcy.github.io/AYS-R-Coding-SPR-2020/
3 stars 3 forks source link

Lab 5 - Average Casualties per Harmful Accident #22

Open SalvadorW2 opened 3 years ago

SalvadorW2 commented 3 years ago

How do I add the total number of incidents and the total number of casualties in order to get the average?

dat %>% select(Incidentid, Totalinjuries, Totalfatalities, hour) %>% mutate(casualties = Totalinjuries + Totalfatalities, harmful = ifelse(casualties > 0, TRUE, FALSE)) %>% select(Incidentid, casualties) %>% group_by(Incidentid, casualties)

Am I even going about this the right way?

jamisoncrawford commented 3 years ago

Am I even going about this the right way?

Besides posting to GitHub two hours before the deadline? 😄

Drop the call to select() as it's unnecessary. Instead, replace it with filter() with Totalinjuries greater than zero or Totalfatalities greater than zero.

Your mutate() call looks good - stick with that.

Afterwards, use group_by() on hour and use summarize() to find the sum of casualties (with sum()) divided by n(). That should get you there.

SalvadorW2 commented 3 years ago

Sorry about that.

I had tried just about everything I could think of and was wondering if anyone else was still online.

jamisoncrawford commented 3 years ago

@SalvadorW2 you're totally fine! Just having fun. You did very well on the lab and I'm excited to hear that you'd like to train further in R after the course. You've got the knack to hack.