I have a dataset with a group column and another column cyl which only has two values. I would like to calculate the proportion of one of the values from cyl for each of the groups in group.
I can do this in multiple steps involving creating new datasets and using full_join however I am wondering if there is a more efficient way to do this as the dataset I work with are large.
library(dplyr)
dat <- mtcars %>% filter(cyl >=6)
dat$group <- seq(1:3)
cyl_6 <- dat %>% filter(cyl == 6) %>% group_by(group) %>%
summarise(count_6 = n())
cyl_8 <- dat %>% filter(cyl == 8) %>% group_by(group) %>%
summarise(count_8 =n())
cyl_data <- cyl_6 %>% full_join(cyl_8) %>%
mutate(six_cyl_prop = count_6/(count_6 + count_8))
cyl_data
group six_cyl_prop
1 0.286
2 0.571
3 0.143