I'm trying to create a summary table that gives me the proportion of yes responses for 17 questions sorted by year. I just don't know how to apply the summarize operation to multiple columns easily without hard-coding it.
Unfortunately, I can't use the summarize_at or summarize_all functions because I'm working with a dataframe. I was thinking of writing a function, looping through the columns, and rbinding the summary columns together, but summarize is a bit weird with column names, which can't be of type character. What do you recommend?
Here's what I currently have:
s2 <- db %>%
group_by(Year)%>%
summarize(Q1=round(sum(Q1d, na.rm=TRUE)*100/length(which(!is.na(Q1d))),1),
Q2=round(sum(Q2d, na.rm=TRUE)*100/length(which(!is.na(Q2d))),1),
Q3=round(sum(Q3d, na.rm=TRUE)*100/length(which(!is.na(Q3d))),1),
Q4=round(sum(Q4d, na.rm=TRUE)*100/length(which(!is.na(Q4d))),1),
Q5=round(sum(Q5d, na.rm=TRUE)*100/length(which(!is.na(Q5d))),1),
Q6=round(sum(Q6d, na.rm=TRUE)*100/length(which(!is.na(Q6d))),1),
Q7=round(sum(Q7d, na.rm=TRUE)*100/length(which(!is.na(Q7d))),1),
Q8=round(sum(Q8d, na.rm=TRUE)*100/length(which(!is.na(Q8d))),1),
Q9=round(sum(Q9d, na.rm=TRUE)*100/length(which(!is.na(Q9d))),1),
Q10=round(sum(Q10d, na.rm=TRUE)*100/length(which(!is.na(Q10d))),1),
Q11=round(sum(Q11d, na.rm=TRUE)*100/length(which(!is.na(Q11d))),1),
Q12=round(sum(Q12d, na.rm=TRUE)*100/length(which(!is.na(Q12d))),1),
Q13=round(sum(Q13d, na.rm=TRUE)*100/length(which(!is.na(Q13d))),1),
Q14=round(sum(Q14d, na.rm=TRUE)*100/length(which(!is.na(Q14d))),1),
Q15=round(sum(Q15d, na.rm=TRUE)*100/length(which(!is.na(Q15d))),1),
Q16=round(sum(Q16d, na.rm=TRUE)*100/length(which(!is.na(Q16d))),1),
Q17=round(sum(Q17d, na.rm=TRUE)*100/length(which(!is.na(Q17d))),1),
)
Note: Q1d, Q2d... are the names of the columns