DS4PS / course_website

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

Trying to recreate q4 in Lab 9 #20

Open BissKuttner opened 5 years ago

BissKuttner commented 5 years ago

Im trying to recreate lab 9 and am not able to get the answer for question 4, "Of all of the accidents that result in injuries or fatalities, how often are alcohol OR drugs involved?"

This is the code I have entered and I have it all in one code block because the variables I created by mutating don't exist if I put

sum (dat$injury.fatality & dat$drugs.alcohol)/ sum (dat$injury.fatality)

in its own code block.

What am I doing wrong??


# mutate to create a variable for accidents with injuries and fatalities and one for drug and alcohol use

dat%>%
  mutate( 
    injury.fatality = dat$Totalinjuries > 0 | dat$Totalfatalities > 0,
drugs.alcohol = 
            dat$AlcoholUse_Drv1 == "Alcohol" |
            dat$AlcoholUse_Drv2 == "Alcohol" |
            dat$DrugUse_Drv1 == "Drugs" |
            dat$DrugUse_Drv2 == "Drugs" )%>%

  count( injury.fatality, drugs.alcohol )

sum (dat$injury.fatality & dat$drugs.alcohol)/ sum (dat$injury.fatality)
lecy commented 5 years ago

In order to save the results from the mutate function you need to assign them back to a new variable (or the same variable). You are missing this first line:

dat <- 
  dat %>%
  mutate( injury.fatality = Totalinjuries > 0 | Totalfatalities > 0, 
            drugs.alcohol = AlcoholUse_Drv1 == "Alcohol" | 
            AlcoholUse_Drv2 == "Alcohol" | 
            DrugUse_Drv1 == "Drugs" | 
            DrugUse_Drv2 == "Drugs" )