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 Question 2.2 #38

Open zbell3 opened 2 years ago

zbell3 commented 2 years ago

The numbers in my table aren't coming out the same as the instructions. My code seems to be working but I'm not sure where I'm going wrong. Has anyone else had this issue?

I'm using the following code: dat %>% count(age, hour12) %>% group_by(age) %>% mutate(n.age = sum(n)) %>% mutate(p.age = n / n.age) %>% ungroup() %>% group_by(hour12) %>% mutate(n.hour = sum(n)) %>% mutate(p.hour = n / n.hour) %>% ungroup() %>% filter(hour12 == "7 AM")

and I get this: image

jamisoncrawford commented 2 years ago

@zbell3 can you be more specific?

zbell3 commented 2 years ago

Sure. Below are numbers from the instructions. You can see the n.hour you found was 1606 and mine is 2758. Also my "n"s are higher than yours, which throws off all the percentages. So I know somewhere in my code I'm messing up, but I can get it to at least run. image

jamisoncrawford commented 2 years ago

For the sake of time, I have to respond somewhat lazily here and I apologize for that.

Try this - and I've left some things blank as not to give it all away!

dat %>% 
  group_by(hour12, age) %>% 
  summarize(n = n()) %>% 
  group_by(age) %>% 
  mutate(n.age = sum(___)) %>%
  mutate(prop.age = ___ / ___)) %>% 
  ungroup() %>%
  filter(___ == "___")

Please let me know if this helps. Looks like you are really mastering chaining operations in dplyr!

zbell3 commented 2 years ago

Hmm thank you, the numbers are still not coming out right for me. Based on your help here and the help from the instructions (I added below) I think maybe my "mutate (n.age = sum(n)) %>%" isn't correct. However I can't sum "age" as it is categorical. I'm also not sure what I need to mutate to create n.hour. Otherwise I think my code is correct.

help from the instructions: d %>% count( f1, f2 ) %>% group_by( f1 ) %>% mutate( n.f1 = sum(n) ) %>% mutate( prop= round( n / n.f1, 2 ) ) %>% filter( f2 == "L-01" )

This is what I'm running now and I'm still getting the same numbers from my first attempt (the one's that are wonky): dat %>% group_by(hour12, age) %>% summarize(n = n()) %>% group_by(age) %>% mutate(n.age = sum(n)) %>% mutate(prop.age = n / n.age) %>% group_by(hour12) %>% mutate(n.hour = sum(n)) %>% mutate(p.hour = n / n.hour) %>% ungroup() %>% filter(hour12 == "7 AM")

Maybe I'll just wait for the solutions.