Learn R Programming

sjmisc (version 2.2.0)

var_labels: Add variable label(s) to variables

Description

This function adds variable labels as label-attribute (named "label") to a set of variables in a data frame. Unlike set_label, this function takes several pairs of named vectors as input to change multiple variable labels in a data frame at once. Furthermore, since the first argument is a data frame and multiple variables can be passed as arguments, this function fits nicely into a pipe-workflow.

Usage

var_labels(data, ...)

Arguments

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

Value

data, with variable label attribute(s) for those variables specified in ....

Examples

Run this code
library(dplyr)

# 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 %>%
  var_labels(a = "First variable", c = "third variable") %>%
  get_label()

Run the code above in your browser using DataLab