I have a large dataset for which I need to generate multiple cross-tables. These are particularly two dimensional tables to generate frequencies along with mean and SD.
So give an example I have the below data -
City <- c("A","B","A","A","B","C","D","A","D","C")
Q1 <- c("Agree","Agree","Agree","Agree","Agree","Neither","Neither","Disagree","Agree","Agree")
df <- data.frame(City,Q1)
Keeping the data in mind, I want to generate a cross-table with mean as below -
City
A B C D
Agree 3 2 1 1
Neither 1 1
Disagree 1
Total 4 2 2 2
Mean 2.5 3 2.5 2.5
When generating the mean, Agree is given a weight of 3, Neither is given a weight of 2 and Disagree is a given a weight of 1. The cross-table output should have the mean just below the Total column. It would be good to have gridlines between each column and row.
Can you please suggest how to achieve this in R?