How can I calculate the proportion of "yes" for each section
library(dplyr)
attendance <- c("No", "Yes", "No", "Yes", "Yes", "No", "Yes")
section <- c("A", "A", "B", "B", "B", "B", "B")
tbl <- tibble(attendance, section)
How can I calculate the proportion of "yes" for each section
library(dplyr)
attendance <- c("No", "Yes", "No", "Yes", "Yes", "No", "Yes")
section <- c("A", "A", "B", "B", "B", "B", "B")
tbl <- tibble(attendance, section)
tbl %>%
group_by(section) %>%
summarise(prop = sum(attendance == "Yes")/ n())
section prop
<chr> <dbl>
1 A 0.5
2 B 0.6
You can use -
library(dplyr)
tbl %>% group_by(section) %>% summarise(prop = mean(attendance == 'Yes'))
# section prop
# <chr> <dbl>
#1 A 0.5
#2 B 0.6
Or in base R -
aggregate(attendance~section, tbl, function(x) mean(x == 'Yes'))