I just recently started using R, and it's also my first time posting here (sorry if I miss a few details my question). I have a dataset that contains around 22 questions/statements that are in 5 pt. Likert-scale format. Each statement has a dedicated column with the respective answers under it.
Here is a sample data frame of what it looks like (but with only 3 columns instead of 22):
q1 = c(1, 2, 2, 1, 3, 4, 3, 5, 2, 2)
q2 = c(2, 3, 5, 5, 4, 5, 1, 1, 5, 3)
q3 = c(4, 4, 2, 3, 2, 1, 1, 1, 5, 5)
data <- data.frame(q1, q2, q3)
colnames(data) = c("This is statement 1.", "This is statement 2.", "This is statement 3.")
data
I have a few specific requirements for it:
- It needs to be horizontal, so that each statement and its responses will form one row. It would be too long if I have it set vertically for each of the 22 questions.
- If possible, it should be compatible with knitr :: kable (compatible meaning it looks decent when knit in R Markdown)
- Each element in the data frame should have a corresponding row percentage enclosed in parenthesis.
- The header of the table should be 1, 2, 3, 4, 5 (since they are all 5 pt. Likert scale questions)
CodePudding user response:
In base R
, just do
tbl1 <- table(stack(data)[2:1] )
tbl2 <- proportions(tbl1, 1) * 100
tbl1[] <- sprintf('%d (%d%%)', tbl1, tbl2)
-output
tbl1
values
ind 1 2 3 4 5
This is statement 1. 2 (20%) 4 (40%) 2 (20%) 1 (10%) 1 (10%)
This is statement 2. 2 (20%) 1 (10%) 2 (20%) 1 (10%) 4 (40%)
This is statement 3. 3 (30%) 2 (20%) 1 (10%) 2 (20%) 2 (20%)