It is kind of tedious to do it this way, but I have no nicer solution in mind right now:
# fake data
set.seed(1)
z <- data.frame(matrix(ncol = 3, nrow = 2000))
names(z) <- c('can_a', 'can_b', 'can_c')
z$can_a <- sample(x = c(0:10, 88, 99), size = nrow(z), replace = TRUE)
z$can_b <- sample(x = c(0:10, 88, 99), size = nrow(z), replace = TRUE)
z$can_c <- sample(x = c(0:10, 88, 99), size = nrow(z), replace = TRUE)
# corrected the labels, since before they coerced to NAs
z$can_a <- factor(z$can_a, levels = c(0:10, 88, 99),
c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
'DonĀ“t know', 'No answer'))
z$can_b <- factor(z$can_b, levels = c(0:10, 88, 99),
c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
'DonĀ“t know', 'No answer'))
z$can_c <- factor(z$can_c, levels = c(0:10, 88, 99),
c('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
'DonĀ“t know', 'No answer'))
####
library(dplyr); library(tidyr)
# calculate frequencies
frequencies <- z |>
pivot_longer(cols = everything(), names_to = 'can', values_to = 'value') |>
group_by(can,value) |>
summarise(
frequency = n()/nrow(z)
) |> ungroup() |>
pivot_wider(values_from = 'frequency', names_from = 'can')
#> `summarise()` has grouped output by 'can'. You can override using the `.groups`
#> argument.
# get the mean additionally
the_means <- z |>
pivot_longer(cols = everything(), names_to = 'can', values_to = 'value') |>
filter(value %in% 0:10) |>
group_by(can) |>
summarise(
mean = mean(as.numeric(value))
) |> ungroup() |>
pivot_wider(values_from = 'mean', names_from = 'can') |>
# add a column "value" for binding later
mutate(value = 'mean')
# put together
result <- bind_rows(the_means,frequencies) |> select(value,everything())
result
#> # A tibble: 14 Ć 4
#> value can_a can_b can_c
#> <chr> <dbl> <dbl> <dbl>
#> 1 mean 6.11 6.08 5.99
#> 2 0 0.066 0.071 0.076
#> 3 1 0.0705 0.0755 0.076
#> 4 2 0.0765 0.0685 0.0825
#> 5 3 0.0785 0.0765 0.0755
#> 6 4 0.082 0.075 0.072
#> 7 5 0.0785 0.0765 0.077
#> 8 6 0.081 0.083 0.073
#> 9 7 0.075 0.079 0.071
#> 10 8 0.0885 0.073 0.081
#> 11 9 0.0835 0.0855 0.0735
#> 12 10 0.0685 0.071 0.079
#> 13 DonĀ“t know 0.076 0.078 0.079
#> 14 No answer 0.0755 0.0875 0.0845
Created on 2022-09-03 by the reprex package (v2.0.1)
I have corrected the creation of your factor variables above, since you coerced NA
s beforehand. I hope it fulfills your needs.
Kind regards