How to create a grouped frequency distribution table for likert scale in R (Studio)

84 views Asked by At

I have a dataset from limesurvey, containing different variables/items from likert scales and some of the variables depend to a grouping variable, e.g:

Question A: I visited the following workshop: [list of 8, select one of them]

  • Workshop 1 "school bullying"
  • Workshop 2 "teaching online"
  • ...
  • Workshop 8 "learning methods"

Question B: [likert items with 4 choices "yes (++), more yes (+), more no (-), no (--)"]

  • I liked the workshop.
  • I learned a lot.
  • The teacher was brilliant.
  • I need more input.
  • The given materials were useful.

I have found a method to create the Table for Items from "question B" - my problem is: How can I create such a table for every single factor-level from "question A"?

Now I want to create a result table for every Workshop.

(expected tables:) Results Workshop 1:

Item yes (++) more yes (+) more no (-) no (--)
1. I liked the workshop. 10 5 1 1
2. I learned a lot. 5 10 2 0
3. The teacher was brilliant. 1 10 5 1
4. I need more input. 1 1 10 5
5. The given materials were useful. 10 1 5 1

Results Workshop 2:

Item yes (++) more yes (+) more no (-) no (--)
1. I liked the workshop. 20 7 3 1
2. I learned a lot. 5 22 2 0
3. The teacher was brilliant. 1 11 4 1
4. I need more input. 0 1 13 5
5. The given materials were useful. 17 1 4 1

Here is my "dataset.csv"

"I took part in the following workshop in the morning:"," [I liked the workshop.]"," [I learned a lot.]"," [The teacher was brilliant.]"," [I need more input.]"," [The given materials were useful.]"
"1","3","2","2","1","1"
"1","3","2","1","0","0"
"1","0","1","2","3","3"
"1","3","2","1","2","2"
"1","3","3","3","3","3"
"2","3","3","3","3","3"
"2","3","2","2","2","2"
"2","3","1","1","2","0"
"2","1","1","1","1","1"
"2","1","2","2","2","3"
"3","3","1","1","0","0"
"3","0","0","0","0","0"
"3","1","1","1","1","2"
"4","3","2","1","1","1"
"4","3","3","2","1","1"
"4","2","2","2","1","2"
"5","0","1","2","3","3"
"6","1","2","2","1","1"
"5","2","2","2","2","2"
"6","1","2","2","2","2"
"2","3","3","3","3","3"
"4","1","2","2","1","1"
"5","0","0","0","0","0"
"5","1","1","0","1","1"
"6","2","1","2","1","0"

Here’s my Markdown:

---
title: "Report"
#author: ""
#date: "`r format(Sys.Date(),format='%x')`"
output: pdf_document
classoption: landscape

knit: (function(inputFile, encoding) { 

    rmarkdown::render(
        inputFile, encoding = encoding, 
        output_file = file.path(
            dirname(inputFile), paste0(Sys.Date(), '_workshops.pdf'))) 
    })
---


```{r load-packages, echo=FALSE, results='hide', message=FALSE, warning=FALSE}
library("likert")
library("grid")
library('unikn')

library(tidyverse)
library(kableExtra)
library(knitr)
library(tinytex)

library(crosstable)
library(ggplot2)
library(data.table)
library(xtable)
library(flextable)
library(plyr)


```


```{r, echo=FALSE, results='hide', message=FALSE}

data <- read.csv("C:/Users/.../dataset.csv", quote = "'\"", na.strings=c("", "\"\""), stringsAsFactors=FALSE, fileEncoding="UTF-8-BOM")

# LimeSurvey Field type: F
data[, 1] <- as.numeric(data[, 1])
attributes(data)$variable.labels[1] <- "I took part in the following workshop in the morning:"
data[, 1] <- factor(data[, 1], levels=c(1,2,3,4,5,6),labels=c("school bullying", "teaching online", "overhead projector", "future of reading", "teaching in bad mood", "learning methods"))
names(data)[1] <- "Workshop"
# LimeSurvey Field type: F
data[, 2] <- as.numeric(data[, 2])
attributes(data)$variable.labels[2] <- "[I liked the workshop.]"
data[, 2] <- factor(data[, 2], levels=c(3,2,1,0),labels=c("totally yes(++)", "yes(+)", "no(-)", "totally no(--)"))
names(data)[2] <- "item_1"
# LimeSurvey Field type: F
data[, 3] <- as.numeric(data[, 3])
attributes(data)$variable.labels[3] <- "[I learned a lot.]"
data[, 3] <- factor(data[, 3], levels=c(3,2,1,0),labels=c("totally yes(++)", "yes(+)", "no(-)", "totally no(--)"))
names(data)[3] <- "item_2"
# LimeSurvey Field type: F
data[, 4] <- as.numeric(data[, 4])
attributes(data)$variable.labels[4] <- "[The teacher was brilliant.]"
data[, 4] <- factor(data[, 4], levels=c(3,2,1,0),labels=c("totally yes(++)", "yes(+)", "no(-)", "totally no(--)"))
names(data)[4] <- "item_3"
# LimeSurvey Field type: F
data[, 5] <- as.numeric(data[, 5])
attributes(data)$variable.labels[5] <- "[I need more input.]"
data[, 5] <- factor(data[, 5], levels=c(3,2,1,0),labels=c("totally yes(++)", "yes(+)", "no(-)", "totally no(--)"))
names(data)[5] <- "item_4"
# LimeSurvey Field type: F
data[, 6] <- as.numeric(data[, 6])
attributes(data)$variable.labels[6] <- "[The given materials were useful.]"
data[, 6] <- factor(data[, 6], levels=c(3,2,1,0),labels=c("totally yes(++)", "yes(+)", "no(-)", "totally no(--)"))
names(data)[6] <- "item_5"




#Workshop
W <- mapply(table, data[, 2:6]); W
W <- t(W)
W <- round((W/rowSums(W))*100,0)

W2 <- as.data.frame(W) %>%
  tibble::rownames_to_column("Question")

tableW <- flextable(W2) %>%
  width(j = 1, width = 12, unit = "cm") %>%  width(j = 2:5, width = 1, unit = "cm")


tableZ <- theme_zebra(tableW, odd_header = "#87ceeb",  odd_body = "transparent",  even_header = "transparent",  even_body = "#d1eeee")


```


\newpage

```{r, fig.width=11,fig.height=5, echo=FALSE}
tableZ
```

So - does anybody have an idea, how to add the function of creating such a result table for every given workshop?

1

There are 1 answers

1
zephryl On

You could split() your data by workshop attended and loop over the results, eg:

# ... 
W2 <- as.data.frame(W) %>%
  tibble::rownames_to_column("Results Workshop")

Tables <- lapply(
  split(W2, W2$WorkshopAttended),
  \(W2a) {
    TableWa <- flextable(W2a) %>%
      width(j = 1, width = 12, unit = "cm") %>%  
      width(j = 2:5, width = 1, unit = "cm")
    
    theme_zebra(TableWa, odd_header = "#87ceeb",  odd_body = "transparent",  even_header = "transparent",  even_body = "#d1eeee")
  }
)

Not tested as you didn’t provide sample data.