R/varlabels.R
varlabels.Rd
In a surveydata object, the variable.labels
attribute store metadata about the original question text (see foreign::read.spss()
for details). The function varlabels()
returns the variable.labels
attribute of data, and varlabels(x) <- value
updates this attribute.
varlabels(x)
varlabels(x) <- value
surveydata object
New value
In a surveydata object, the varlabels
attribute is a named character vector, where the names correspond to the names of the the columns in
# Extract the variable labels from membersurvey
ms <- membersurvey[, c("id", "Q1", "Q2")]
str(ms)
#> Classes ‘surveydata’ and 'data.frame': 215 obs. of 4 variables:
#> $ id : num 3 5 6 11 13 15 21 22 23 25 ...
#> $ Q1_1: num 8 35 34 20 20 36 12 11 18 24 ...
#> $ Q1_2: num 2 12 12 9 3 20 2.5 0.5 3 8 ...
#> $ Q2 : Ord.factor w/ 10 levels "Before 2002"<..: 8 1 1 9 9 1 8 10 7 5 ...
#> - attr(*, "variable.labels")= Named chr [1:4] "id" "[A market research professional] How many years of experience do you have as ?" "[An independent consultant / small business] How many years of experience do you have as ?" "In what year did you first join the organisation?"
#> ..- attr(*, "names")= chr [1:4] "id" "Q1_1" "Q1_2" "Q2"
#> - attr(*, "pattern")=List of 2
#> ..$ sep : chr "_"
#> ..$ exclude: chr "other"
varlabels(ms)
#> id
#> "id"
#> Q1_1
#> "[A market research professional] How many years of experience do you have as ?"
#> Q1_2
#> "[An independent consultant / small business] How many years of experience do you have as ?"
#> Q2
#> "In what year did you first join the organisation?"
varlabels(ms)["Q2"]
#> Q2
#> "In what year did you first join the organisation?"
# Assign a new value to the text of question 2
varlabels(ms)["Q2"] <- "When did you join?"
varlabels(ms)
#> id
#> "id"
#> Q1_1
#> "[A market research professional] How many years of experience do you have as ?"
#> Q1_2
#> "[An independent consultant / small business] How many years of experience do you have as ?"
#> Q2
#> "When did you join?"
str(ms["Q2"])
#> Classes ‘surveydata’ and 'data.frame': 215 obs. of 1 variable:
#> $ Q2: Ord.factor w/ 10 levels "Before 2002"<..: 8 1 1 9 9 1 8 10 7 5 ...
#> - attr(*, "variable.labels")= Named chr "When did you join?"
#> ..- attr(*, "names")= chr "Q2"
#> - attr(*, "pattern")=List of 2
#> ..$ sep : chr "_"
#> ..$ exclude: chr "other"