DS4PS / course_website

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

Lab 12 Q 2 #28

Open BissKuttner opened 5 years ago

BissKuttner commented 5 years ago

I cant figure this out. I have created a variable to get a count for accidents on Mondays I thought I had generated avg accidents per week I dont know how to combine these two. Nothing I do seems to work.

TO GET TO ACCIDENTS ON MONDAYS

d2 <- 
  dat %>%
  mutate (accidents.mon = as.character(day) == "Mon")

count(dat,accidents.mon)

TO GET AVERAGE CRASHES PER WEEK

d2 <- 
  dat %>% 
  filter( as.numeric(week) <= 52 ) %>%
  group_by( Year ) %>%
  count( week ) %>%
  group_by( week ) %>%
  summarize( ave.crashes.per.week = mean(n) )
lecy commented 5 years ago

These are actually easier using simple logical statements. Recall that if we can use a logical statement to specify the group we want, then we can easily count group members and find the proportion of the total:

sum( dat$gender == "male" )   # total number of males in the study
mean( dat$gender == "male" )  # proportion of study group that is male
BissKuttner commented 5 years ago

That makes sense. What confuses me is that the question is asking for "each week." If I take that mean, am I getting the answer for each week or the mean of the total of Mondays?

BissKuttner commented 5 years ago

I think I have it.

lecy commented 5 years ago

You are right, it's a little ambiguous.

If you calculate the proportion for each week of the year, you would need to group by week, something like:

dat %>%
group_by( week ) %>%
summarize( ave.on.mondays = mean( day == "Mon" )  )