I have a survey dataset with 40 ordered factor variables. The variables are transformed into characters when the data is imported.Please correct me if I am wrong, as I am thinking of using the apply function here.
Below my data manipulation:
### data
v1 <- as.character(c(1,4,2,4,3,1,3,4,5,2,2,3,6,5,4,6,5,4,5,6,6,2,4,3,4,5,6,1,6,3,5,6,3,2,4,5,3,2,4,5,3,2,4))
v2 <- as.character(c(3,4,1,4,5,1,3,1,5,6,4,3,4,5,6,3,3,5,4,3,3,5,6,3,4,3,4,6,3,1,1,3,4,5,6,1,3,6,4,3,1,6,5))
df <- data.frame(v1,v2)
### transform into ordered factor
df$v1.f <- as.factor(df$v1)
df$v1.f <- ordered(df$v1.f, levels = c("1", "2", "3", "4", "5", "6"))
The real levels are unsorted characters, which is why I included the step. I don't mind typing this for all variables, but it seems redundant.
My second issue is with the output. I would like to create a fancy report and know how to generate the numbers for it:
v1.freq <- table(df$v1.f)
v1.perc <- round(prop.table(v1.freq),2)*100
v1.med <- median(df$v1)
How can a table that contains all the information for all the variables at once for multiple variables be printed - especially when there are no answers to a level (see v2, where there is no response for level 2; table() simply skips over the level).
How do I turn the R output in a table that has the levels as headers and frequencies and percentages as rows for multiple variables?
Copy/pasting the numbers into an Excel Sheet seems - again - unnecessary and prone to errors.