Often, SPSS files will come with a label hidden in the
attr(which = "label") parameter. This function extracts only the relevant
part of the label that is often needed (e.g. without the Q1 part)
Arguments
- x
The labelled object to extract the label from.
- split
Logical. Whether the label needs to be split based on a separator.
- sep
The seperator character(s) to split the label on if split = TRUE. Should include spaces if applicable.
- position
Position of the label that you need. E.g. if the label is: "Q1 - What is the label?", you will need the 2nd part after the split
Examples
x <- "Labelled vector"
attr(x, which = "label") <- "Q1 - What is the label?"
srv_extract_label(x, split = TRUE, sep = " - ", position = 2)
#> [1] "What is the label?"
# If you want to extract multiple parts (e.g. you want to keep the question
# number) in the label below, you can supply multiple positions. It will
# collapse with a '_'
x <- "Labelled vector"
attr(x, which = "label") <- "Start of the question - Q1 - What is the label?"
srv_extract_label(x, split = TRUE, sep = " - ", position = c(2, 3))
#> [1] "Q1_What is the label?"