Hi R masters,
I have written following code to calculate average scores for each question and for each gender (and total):
response.data <- data.frame(
A_Top2 = c(NA, 0, 100, 0, 0, 0, 0, 0, 0, 0, 100, 100, 0, NA),
B_Top2 = c(0, 0, 100, 100, 0, 0, 0, 0, 100, 0, 0, 0, 100, 100),
C_Yes = c(NA, 0, 0, 100, 100, 0, 0, NA, 0, 0, 0, 0, 100, 100),
D_Yes = c(0, 0, 0, 100, 0, 100, 0, 0, 100, 0, 0, 0, 100, NA),
URN = as.factor(c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg",
"hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn")),
Gender = as.factor(c("Male", "Male", "Male", "Male", "Male", "Male",
"Female", "Female", "Female", "Female", "Female",
"Female", "Male", "Male"))
)
response.data
response.data %>%
bind_rows(mutate(.data = .,
Gender = "Total")) %>%
group_by(Gender) %>%
summarise_at(.vars = vars(ends_with(match = "Top2"), ends_with(match = "Yes")),
.funs = list(Aver = mean,
Count = ~n())) %>%
print(width = Inf)
Unfortunately, results are incorrect. Some Averages are blank. Also, count should take into account valid responses for each question (Only Question B_Top2 is answered by all 14 respondents).
Do you know what I am doing wrong?