This function adds variable labels as attribute (named "label") to the variable x, resp. to a set of variables in a data frame or a list-object. var_labels() is intended for use within pipe-workflows and has a tidyverse-consistent syntax, including support for quasi-quotation (see 'Examples').

set_label(x, label)

set_label(x) <- value

var_labels(x, ...)

Arguments

x

Variable (vector), list of variables or a data frame where variables labels should be added as attribute. For var_labels(), x must be a data frame only.

label

If x is a vector (single variable), use a single character string with the variable label for x. If x is a data frame, use a vector with character labels of same length as ncol(x). Use label = "" to remove labels-attribute from x, resp. set any value of vector label to "" to remove specific variable label attributes from a data frame's variable.

value

See label.

...

Pairs of named vectors, where the name equals the variable name, which should be labelled, and the value is the new variable label.

Value

x, with variable label attribute(s), which contains the variable name(s); or with removed label-attribute if

label = "".

See also

See vignette Labelled Data and the sjlabelled-Package for more details; set_labels to manually set value labels or get_label to get variable labels.

Examples

# manually set value and variable labels
dummy <- sample(1:4, 40, replace = TRUE)
dummy <- set_labels(dummy, labels = c("very low", "low", "mid", "hi"))
dummy <- set_label(dummy, label = "Dummy-variable")

# or use:
# set_label(dummy) <- "Dummy-variable"

# Set variable labels for data frame
dummy <- data.frame(
  a = sample(1:4, 10, replace = TRUE),
  b = sample(1:4, 10, replace = TRUE),
  c = sample(1:4, 10, replace = TRUE)
)
dummy <- set_label(dummy, c("Variable A", "Variable B", "Variable C"))
str(dummy)
#> 'data.frame':	10 obs. of  3 variables:
#>  $ a: int  2 1 2 3 2 4 1 2 1 4
#>   ..- attr(*, "label")= Named chr "Variable A"
#>   .. ..- attr(*, "names")= chr "a"
#>  $ b: int  4 2 3 1 2 2 3 2 1 1
#>   ..- attr(*, "label")= Named chr "Variable B"
#>   .. ..- attr(*, "names")= chr "b"
#>  $ c: int  3 2 1 4 1 4 4 3 4 4
#>   ..- attr(*, "label")= Named chr "Variable C"
#>   .. ..- attr(*, "names")= chr "c"

# remove one variable label
dummy <- set_label(dummy, c("Variable A", "", "Variable C"))
str(dummy)
#> 'data.frame':	10 obs. of  3 variables:
#>  $ a: int  2 1 2 3 2 4 1 2 1 4
#>   ..- attr(*, "label")= Named chr "Variable A"
#>   .. ..- attr(*, "names")= chr "a"
#>  $ b: int  4 2 3 1 2 2 3 2 1 1
#>  $ c: int  3 2 1 4 1 4 4 3 4 4
#>   ..- attr(*, "label")= Named chr "Variable C"
#>   .. ..- attr(*, "names")= chr "c"

# setting same variable labels to multiple vectors

# create a set of dummy variables
dummy1 <- sample(1:4, 40, replace = TRUE)
dummy2 <- sample(1:4, 40, replace = TRUE)
dummy3 <- sample(1:4, 40, replace = TRUE)
# put them in list-object
dummies <- list(dummy1, dummy2, dummy3)
# and set variable labels for all three dummies
dummies <- set_label(dummies, c("First Dummy", "2nd Dummy", "Third dummy"))
# see result...
get_label(dummies)
#> [1] "First Dummy" "2nd Dummy"   "Third dummy"


# use 'var_labels()' to set labels within a pipe-workflow, and
# when you need "tidyverse-consistent" api.
# Set variable labels for data frame
dummy <- data.frame(
  a = sample(1:4, 10, replace = TRUE),
  b = sample(1:4, 10, replace = TRUE),
  c = sample(1:4, 10, replace = TRUE)
)

if (require("magrittr") && require("rlang")) {
  dummy %>%
    var_labels(a = "First variable", c = "third variable") %>%
    get_label()

  # with quasi-quotation
  v1 <- "First variable"
  v2 <- "Third variable"
  dummy %>%
    var_labels(a = !!v1, c = !!v2) %>%
    get_label()

  x1 <- "a"
  x2 <- "c"
  dummy %>%
    var_labels(!!x1 := !!v1, !!x2 := !!v2) %>%
    get_label()
}
#> Loading required package: rlang
#> 
#> Attaching package: ‘rlang’
#> The following object is masked from ‘package:magrittr’:
#> 
#>     set_names
#> The following object is masked from ‘package:sjmisc’:
#> 
#>     is_empty
#> The following objects are masked from ‘package:sjlabelled’:
#> 
#>     as_character, as_label
#>                a                b                c 
#> "First variable"               "" "Third variable"