There are several problems with your code
You should be using dplyr for this, not plyr and if you want to use the pivot_ functions you also have to load tidyr
You have forgotten that tidyverse functions don't perform in-place modifications, so this line essentially does nothing and I think is unnecessary.
Obviously, without the previous renaming taking effect, this is not going to work
Here you are using the wrong syntax, if you want to pass multiple values to the cols argument, then you have to use a vector c(-Gender, -Type)
Solving all the syntax and logic problems, the code works:
library(dplyr)
library(tidyr)
response.data <- data.frame(stringsAsFactors = FALSE,
Blank = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0),
Oil = c(0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1),
MOT = c(0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1),
Rep = c(0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1),
Other = c(1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0),
URN = c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg",
"hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn",
"ooo", "ppp"),
Gender = c("Male", "Male", "Male", "Male", "Male", "Male",
"Female", "Female", "Female", "Female", "Female",
"Female", "Male", "Male", "Male", "Male"),
Type = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B",
"B", "B", "A", "A", "A", "A")
)
response.data %>%
select(Type, Gender, everything(), -URN) %>%
group_by(Type, Gender) %>%
summarise_all(list(Count = sum, Proportion = mean)) %>%
pivot_longer(cols = c(-Gender, -Type),
names_to = c("Category", "summary"),
names_sep = "_", "value") %>%
pivot_wider(names_from = summary, values_from = value)
#> # A tibble: 15 x 5
#> # Groups: Type [2]
#> Type Gender Category Count Proportion
#> <chr> <chr> <chr> <dbl> <dbl>
#> 1 A Male Blank 2 0.222
#> 2 A Male Oil 4 0.444
#> 3 A Male MOT 5 0.556
#> 4 A Male Rep 3 0.333
#> 5 A Male Other 2 0.222
#> 6 B Female Blank 1 0.167
#> 7 B Female Oil 1 0.167
#> 8 B Female MOT 0 0
#> 9 B Female Rep 1 0.167
#> 10 B Female Other 4 0.667
#> 11 B Male Blank 0 0
#> 12 B Male Oil 0 0
#> 13 B Male MOT 0 0
#> 14 B Male Rep 1 1
#> 15 B Male Other 0 0