Skip to contents

This function makes it easy to get the variable labels from either an individual vector or a data frame. NOTE: it is not possible to set or modify the variable labels with this function.

Usage

attr_var_label(x, data, unlist)

Arguments

x

A vector object, the name of a column in a data.frame, or an an actual data.frame object.

data

A data.frame or tibble object. This should only be specified when x is only the name of a column in a data.frame.

unlist

Logical. If TRUE, the default, returns a named vector. If FALSE, returns a list. This only works when x is a data.frame

Value

If x is a variable or vector, a string containing the "label" attribute, if one is present, is returned. If x is a data.frame then a named vector with the "label" attribute from each variable is returned.

Examples

library(adlgraphs)
# create a random vector
x <- sample(c(0, 1), replace = TRUE, size = 10)
# add a question preface attribute
attr(x, "question_preface") <- "This is a question preface"
# check to see that there is a new attribute called `question_preface`
attributes(x)
#> $question_preface
#> [1] "This is a question preface"
#> 
# now get the question_preface
attr_question_preface(x)
#> [1] "This is a question preface"

# now let's create a realistic workflow with a data.frame --------
# create a fake dataset
df <- data.frame(
  x_1 = sample(c(0, 1), replace = TRUE, size = 10),
  x_2 = sample(c(0, 1), replace = TRUE, size = 10),
  x_3 = sample(c(0, 1), replace = TRUE, size = 10),
  x_4 = sample(c(0, 1), replace = TRUE, size = 10)
) 

# set the variable labels
attr(df$x_1, "label") <- "Which of the following colors do you like? Blue"
attr(df$x_2, "label") <- "Which of the following colors do you like? Red"
attr(df$x_3, "label") <- "Which of the following colors do you like? Yellow"
attr(df$x_4, "label") <- "Which of the following colors do you like? Purple" 

# now let's check the variable labels
attr_var_label(df)
#>                                                 x_1 
#>   "Which of the following colors do you like? Blue" 
#>                                                 x_2 
#>    "Which of the following colors do you like? Red" 
#>                                                 x_3 
#> "Which of the following colors do you like? Yellow" 
#>                                                 x_4 
#> "Which of the following colors do you like? Purple" 

# this isn't an ideal variable label if we want to show how many people
# like each of the colors, so let's fix that 

# set the value labels (most survey data should have this)
attr(df$x_1, "labels") <- c("Blue" = 1)
attr(df$x_2, "labels") <- c("Red" = 1)
attr(df$x_3, "labels") <- c("Yellow" = 1)
attr(df$x_4, "labels") <- c("Purple" = 1)

# add the question prefaces and update the variable labels for each column in df
for(x in names(df)) {
  df[[x]] <- set_question_preface(x, df)
}

# now let's check the updated variable labels
attr_var_label(df)
#>      x_1      x_2      x_3      x_4 
#>   "Blue"    "Red" "Yellow" "Purple"