Wait I see you want the % in group above total mean not group mean... back in justs a sec
I see now... there are always so many ways to aggregate data I think this gives the results you want but I think there is a better way to find the group members above the mean of the group.
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 %>%
group_by(title_type) %>%
summarise(count = n(),
pct_of_group_over_mean_of_group = 100 * length(which(imdb_rating> mean(imdb_rating))) / 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 50.00000 20
#> 2 Feature film 10 60.00000 50
#> 3 TV Movie 6 33.33333 30