I'm not clear on exactly what output you're after, but some summarise_all()
(or summarise_at()
if you want to explicitly include rather than exclude variables) could be useful:
response.data %>%
select(-URN, -Gender) %>%
summarise_all(list(sum = sum, mean = mean))
Blank_sum Oil_sum MOT_sum Rep_sum Other_sum Blank_mean Oil_mean MOT_mean Rep_mean Other_mean
1 3 5 5 5 6 0.1875 0.3125 0.3125 0.3125 0.375
Combine that with a pivot_longer()
/gather()
followed by pivot_wider()
/spread()
from tidyr
and you're getting close to what you're after:
response.data %>%
select(-URN, -Gender) %>%
summarise_all(list(Count = sum, Proportion = mean)) %>%
pivot_longer(everything(), names_to = c("Category", "summary"), names_sep = "_", "value") %>%
pivot_wider(names_from = summary, values_from = value)
# A tibble: 5 x 3
Category Count Proportion
<chr> <dbl> <dbl>
1 Blank 3 0.188
2 Oil 5 0.312
3 MOT 5 0.312
4 Rep 5 0.312
5 Other 6 0.375
You could also throw in a group_by(Gender)
if you're interested in these values by your Gender variable:
response.data %>%
select(-URN) %>%
group_by(Gender) %>%
summarise_all(list(Count = sum, Proportion = mean)) %>%
pivot_longer(-Gender, names_to = c("Category", "summary"), names_sep = "_", "value") %>%
pivot_wider(names_from = summary, values_from = value)
# A tibble: 10 x 4
Gender Category Count Proportion
<fct> <chr> <dbl> <dbl>
1 Female Blank 1 0.167
2 Female Oil 1 0.167
3 Female MOT 0 0
4 Female Rep 1 0.167
5 Female Other 4 0.667
6 Male Blank 2 0.2
7 Male Oil 4 0.4
8 Male MOT 5 0.5
9 Male Rep 4 0.4
10 Male Other 2 0.2
I'm not sure if this completely solves what you're trying to do, but hopefully it points you in the right direction.