My survey data has columns with headers like item1, item2, item3 and so on. I want to build a data frame for plotting that has all the items and their n, mean and standard deviation. I have tried this one.
tidy_data <- data %>%
select(c(contains("item"))) %>%
pivot_longer(names_to = "question",
values_to = "answer",
cols = all()) %>%
group_by(question) %>%
summarise(
n=n(),
mean=mean(answer),
sd=sd(answer)
)
I think this code has more than one error. I don't know if this cols=all
argument is correct or do I even need it here. Also, I guess that select
part goes wrong. Thanks for your advice!
Here's a data sample
structure(list(item1 = c(1, 1, 1, 1, 2, NA, 1, 1, 1, 1), item2 = c(2,
5, 2, 2, 2, 2, 2, 5, 19, 6), item3 = c(1, 1, 1, 1, 1, 1, 2, 1,
1, 2), item4 = c(1, 1, 1, 1, 1, 1, NA, 1, 1, NA), item5 = c(4,
4, 4, 1, 4, 4, 6, 4, 2, 4)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))