Dear Technocrat,
Thank you so much!
That's a lot easier.
First of all. No, it's 100% not the best way to display the data.
I don't have options here, because I am asked to provide one table for each item (aka one of my cols) displaying the distribution of agreement in the three countries.
Hence, if I can't work out how to loop it, I will need to copy and paste 5 times 79 items individually into a table call. I most well jump out of my office window.
Whether its one extremely long table or separate ones for each column... I have no preference. All I try to avoid is the copy and paste task. I do whatever is easier.
Any suggestions? 
#Packages used
library(likert)
library(tidyverse)
install.packages("xtable")
install.packages("likert")
library(gmodels)
data(pisaitems)
0 Check data
glimpse(pisaitems)
attach(pisaitems)
- The principal idea for the body of the loop is below with Case for the first Item (ST24Q01)
ST24Q01 <- CrossTable(CNT, ST24Q01, digits=2, chisq = TRUE)
ST24Q01
because I need it for all items (column ST24Q01:ST42Q05) I try to write a for loop across the columns, excluding the first one (CNT)
First idea was to write a function (NOT WORKING)
fun.prop <- function(pisaitems[,i]) {
tab[,i] <- round(prop.table(table(CNT,pisaitems[,i]), margin = 1),2 ) %>%
ftable(table[,i])
}
tabs <- apply(pisaitems, fun.prop)
That doesn't work.
So I figured a for loop would be handy and maybe easier, but it didn't work either
for(i in 2:ncol(pisaitems)) {
tab <- table(pisaitems$CNT, pisaitems[,i])
prop <- prop.table(tab, margin = 1)
print(prop)
}
The final product should look like this for all columns (ST24Q01:ST42Q05)
ST24Q01
CNT Strongly disagree Disagree Agree Strongly agree
Canada 0.26 0.35 0.25 0.14
Mexico 0.22 0.37 0.33 0.08
United States 0.17 0.33 0.34 0.16