I think I've got it now
suppressPackageStartupMessages(library(dplyr))
df <- data.frame(stringsAsFactors=FALSE,
title_type = c("Feature film","Feature film", "TV Movie",
"Documentary", "Documentary", "Feature film", "Feature film", "Feature film", "TV Movie", "TV Movie",
"Feature film", "Feature film", "TV Movie",
"Documentary", "Documentary", "Feature film", "Feature film", "Feature film", "TV Movie", "TV Movie"),
imdb_rating = c(2, 3, 2, 10, 8, 3, 3, 2, 8, 5, 2, 3, 2, 10, 8, 3, 3,
2, 8, 5))
df %>%
mutate(above_mean = imdb_rating > mean(imdb_rating)) %>%
group_by(title_type) %>%
summarise(count = n(),
pct_of_group_over_mean_of_group = 100 * sum(above_mean) / count,
pct_all_films = 100 * count / nrow(.))
#> # A tibble: 3 x 4
#> title_type count pct_of_group_over_mean_of_group pct_all_films
#> <chr> <int> <dbl> <dbl>
#> 1 Documentary 4 100.00000 20
#> 2 Feature film 10 0.00000 50
#> 3 TV Movie 6 66.66667 30