Hi, I am summarizing responses to a Likert-style survey item. In some cases, there are item levels (which I coded as factors) that have no responses, but for purposes of summarizing I would like to include them in the resulting table as a 0 (or I suppose NA would be fine too). What might be a good approach for this?
Here is what I an envisioning:
library(tidyverse)
sampsurvey <- data.frame(rating = c("Agree","Strongly Agree", "Strongly Disagree", "Disagree",
"Strongly Disagree", "Agree"))
# Assign factor levels -- 5 levels
sampsurvey$rating <- factor(sampsurvey$rating, levels = c("Strongly Disagree",
"Disagree",
"Neutral",
"Agree",
"Strongly Agree"))
# How do I get the "Neutral" level counted as a zero in this table?
sampsurvey %>% count(rating)
Here is my rather inelegant solution:
### I would think there would be a better way than this?
sampsurvey <- data.frame(rating = c("Agree","Strongly Agree", "Strongly Disagree", "Disagree",
"Strongly Disagree", "Neutral"),
itemcount = c(1, 1, 1, 1, 1, NA))
sampsurvey$rating <- factor(sampsurvey$rating, levels = c("Strongly Disagree",
"Disagree",
"Neutral",
"Agree",
"Strongly Agree"))
sampsurvey %>%
group_by(rating) %>%
summarise(nresponses = sum(itemcount))