Some packages like expss provide mechanisms for providing longer labels to R objects. These labels can be used when labeling plots and tables, for example, without requiring long or awkward variable names. This is an experimental feature and currently only supports expss or any other system that stores a label in the label attribute of a vector.

var_label(x, unlist = FALSE)

var_label(x) <- value

get_variable_labels(x, unlist = FALSE)

var_label(x, unlist = FALSE)

set_variable_labels(.data, ..., .labels = NA, .strict = TRUE)

Arguments

x

a vector or a data.frame

unlist

for data frames, return a named vector instead of a list

value

a character string or NULL to remove the label For data frames, it could also be a named list or a character vector of same length as the number of columns in x.

.data

a data frame

...

name-value pairs of variable labels (see examples)

.labels

variable labels to be applied to the data.frame, using the same syntax as value in var_label(df) <- value.

.strict

should an error be returned if some labels doesn't correspond to a column of x?

Value

set_variable_labels() will return an updated copy of .data.

Details

For data frames, if value is a named list, only elements whose name will match a column of the data frame will be taken into account. If value is a character vector, labels should in the same order as the columns of the data.frame.

Note

These functions are imported from the {labelled} package.

Examples

KF <-
  mosaicData::KidsFeet %>%
  set_variable_labels(
      length      = 'foot length (cm)',
      width       = 'foot width (cm)',
      birthmonth  = 'birth month',
      birthyear   = 'birth year',
      biggerfoot  = 'bigger foot',
      domhand     = 'dominant hand'
  )
KF %>%
  gf_point(length ~ width, color = ~ domhand)

get_variable_labels(KF)
#> $name
#> NULL
#> 
#> $birthmonth
#> [1] "birth month"
#> 
#> $birthyear
#> [1] "birth year"
#> 
#> $length
#> [1] "foot length (cm)"
#> 
#> $width
#> [1] "foot width (cm)"
#> 
#> $sex
#> NULL
#> 
#> $biggerfoot
#> [1] "bigger foot"
#> 
#> $domhand
#> [1] "dominant hand"
#>