Codebook example with Qualtrics dataset

Ruben Arslan

2019-02-21

knit_by_pkgdown <- !is.null(knitr::opts_chunk$get("fig.retina"))
knitr::opts_chunk$set(warning = FALSE, message = FALSE, error = TRUE, echo = TRUE)
pander::panderOptions("table.split.table", Inf)
ggplot2::theme_set(ggplot2::theme_bw())
library(codebook)
library(dplyr)

Here, I try to demonstrate that we can also use the metadata generated by Qualtrics. Because I am not a Qualtrics user, the developer of the qualtRics R package sent me two example files, one of data and one for metadata, which I use here. Usually, you’d import these files directly via his package.

Load

# library(qualtRics) # currently not on CRAN, so commented out
results <- readRDS(system.file("extdata", "ryan.rds", package = "codebook"))
metadata_ex <- readRDS(system.file("extdata", "metadata.rds", package = "codebook"))

Now, we want the metadata not to be just independent of the data frame. It’s easier to change metadata if it is in the form of a list at the dataset level, so let’s use rio::gather_attrs for that.

results <- results %>% rio::gather_attrs()
attributes(results)$label$Q7
##                                                                                  Q7 
## "How reasonable or unreasonable is the cost of courses/tuition at this university?"

Now, we can loop over the metadata list and put it in the right shape to become attributes. This means given each element the name of the variable it refers to.

names(metadata_ex$questions) <- lapply(metadata_ex$questions, function(x) {
  x$questionName
})

We only want the questions that we have data for.

qs <- names(metadata_ex$questions)
qs <- qs[qs %in% names(attributes(results)$label)]

Now, we assign our metadata list to the attributes of the data.frame.

init <- vector("list", ncol(results)) 
names(init) <- names(results)
attributes(results)$item <- init
attributes(results)$item[qs] <- metadata_ex$questions[qs]

And use rio to put the attributes on the question level again.

results <- results %>% rio::spread_attrs()

To keep this example, we select a subset of variables.

results <- results %>% select(ResponseSet, Q7, Q10)
if (!knit_by_pkgdown) knitr::opts_chunk$set(echo = FALSE)

Please note, that we just reuse the metadata column names from Qualtrics. Standardising this across multiple survey providers would be great but requires more knowledge of Qualtrics than I have.

Owing to a bug in pander, the deeply nested attribute structure is currently not shown as a nice list.

## No missing values.

Metadata

Description

Dataset name: MOCK Qualtrics dataset

a MOCK dataset used to show how to import Qualtrics metadata into the codebook R package

Metadata for search engines

  • keywords: ResponseSet, Q7 and Q10

Variables

ResponseSet

ResponseSet

Distribution

0 missing values.

Summary statistics

name data_type missing complete n empty n_unique min max ResponseSet
ResponseSet character 0 2 2 0 1 20 20 ResponseSet

Q7

How reasonable or unreasonable is the cost of courses/tuition at this university?

Distribution

0 missing values.

Summary statistics

name type data_type ordered value_labels missing complete n n_unique top_counts Q7 selector subSelector questionText doesForceResponse questionName
Q7 MC factor TRUE 1. Extremely reasonable,
2. Moderately reasonable,
3. Slightly reasonable,
4. Neither reasonable nor unreasonable,
5. Slightly unreasonable,
6. Moderately unreasonable,
7. Extremely unreasonable
0 2 2 2 Mod: 1, Sli: 1, Ext: 0, Nei: 0 How reasonable or unreasonable is the cost of courses/tuition at this university? SAVR TX How reasonable or unreasonable is the cost of courses/tuition at this university? FALSE Q7

Item

  • questionType:

    • type: MC
    • selector: SAVR
    • subSelector: TX
  • questionText: How reasonable or unreasonable is the cost of courses/tuition at this university?
  • questionLabel:
  • validation:

    • doesForceResponse: FALSE
  • questionName: Q7
  • choices:

    • 1:

      • recode: 1
      • description: Extremely reasonable
      • choiceText: Extremely reasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 2:

      • recode: 2
      • description: Moderately reasonable
      • choiceText: Moderately reasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 3:

      • recode: 3
      • description: Slightly reasonable
      • choiceText: Slightly reasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 4:

      • recode: 4
      • description: Neither reasonable nor unreasonable
      • choiceText: Neither reasonable nor unreasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 5:

      • recode: 5
      • description: Slightly unreasonable
      • choiceText: Slightly unreasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 6:

      • recode: 6
      • description: Moderately unreasonable
      • choiceText: Moderately unreasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 7:

      • recode: 7
      • description: Extremely unreasonable
      • choiceText: Extremely unreasonable
      • imageDescription:
      • variableName:
      • analyze: TRUE

Q10

How helpful or unhelpful is the staff at the on-campus health center?

Distribution

0 missing values.

Summary statistics

name type data_type ordered value_labels missing complete n n_unique top_counts Q10 selector subSelector questionText doesForceResponse questionName
Q10 MC factor TRUE 1. Extremely helpful,
2. Moderately helpful,
3. Slightly helpful,
4. Neither helpful nor unhelpful,
5. Slightly unhelpful,
6. Moderately unhelpful,
7. Extremely unhelpful
0 2 2 2 Mod: 1, Ext: 1, Ext: 0, Sli: 0 How helpful or unhelpful is the staff at the on-campus health center? SAVR TX How helpful or unhelpful is the staff at the on-campus health center? FALSE Q10

Item

  • questionType:

    • type: MC
    • selector: SAVR
    • subSelector: TX
  • questionText: How helpful or unhelpful is the staff at the on-campus health center?
  • questionLabel:
  • validation:

    • doesForceResponse: FALSE
  • questionName: Q10
  • choices:

    • 1:

      • recode: 1
      • description: Extremely helpful
      • choiceText: Extremely helpful
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 2:

      • recode: 2
      • description: Moderately helpful
      • choiceText: Moderately helpful
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 3:

      • recode: 3
      • description: Slightly helpful
      • choiceText: Slightly helpful
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 4:

      • recode: 4
      • description: Neither helpful nor unhelpful
      • choiceText: Neither helpful nor unhelpful
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 5:

      • recode: 5
      • description: Slightly unhelpful
      • choiceText: Slightly unhelpful
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 6:

      • recode: 6
      • description: Moderately unhelpful
      • choiceText: Moderately unhelpful
      • imageDescription:
      • variableName:
      • analyze: TRUE
    • 7:

      • recode: 7
      • description: Extremely unhelpful
      • choiceText: Extremely unhelpful
      • imageDescription:
      • variableName:
      • analyze: TRUE

Missingness report

Codebook table

name type data_type ordered value_labels missing complete n empty n_unique top_counts min max ResponseSet Q7 selector subSelector questionText doesForceResponse questionName Q10
ResponseSet NA character NA NA 0 2 2 0 1 NA 20 20 ResponseSet NA NA NA NA NA NA NA
Q7 MC factor TRUE 1. Extremely reasonable,
2. Moderately reasonable,
3. Slightly reasonable,
4. Neither reasonable nor unreasonable,
5. Slightly unreasonable,
6. Moderately unreasonable,
7. Extremely unreasonable
0 2 2 NA 2 Mod: 1, Sli: 1, Ext: 0, Nei: 0 NA NA NA How reasonable or unreasonable is the cost of courses/tuition at this university? SAVR TX How reasonable or unreasonable is the cost of courses/tuition at this university? FALSE Q7 NA
Q10 MC factor TRUE 1. Extremely helpful,
2. Moderately helpful,
3. Slightly helpful,
4. Neither helpful nor unhelpful,
5. Slightly unhelpful,
6. Moderately unhelpful,
7. Extremely unhelpful
0 2 2 NA 2 Mod: 1, Ext: 1, Ext: 0, Sli: 0 NA NA NA NA SAVR TX How helpful or unhelpful is the staff at the on-campus health center? FALSE Q10 How helpful or unhelpful is the staff at the on-campus health center?