Learn R Programming

sjmisc (version 2.3.0)

set_label: Add variable label(s) to variables

Description

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 (see 'Examples').

Usage

set_label(x, lab, attr.string = NULL)
set_label(x, attr.string = NULL) <- 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.
lab
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 lab = "" to remove labels-attribute from x, resp. set any value of vector lab to "" to remove specific variable label attributes from a data frame's variable.
attr.string
Attribute string for the variable label. Note: Usually, this argument should be ignored. It is only used internally for the write_spss and write_stata functions.
value
See lab.
...
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 lab = "".

Details

See 'Details' in get_labels

See Also

The sjPlot manual on data initialization or inspecting (SPSS imported) data frames for more details; set_labels to manually set value labels or get_label to get variable labels.

Examples

Run this code
# sample data set, imported from SPSS.
data(efc)
frq(efc$e42dep)

# 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, lab = "Dummy-variable")

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

# auto-detection of value labels by default, auto-detection of
# variable labels if argument "title" set to NULL.
## Not run: 
# library(sjPlot)
# sjp.frq(dummy, title = NULL)## End(Not run)


# 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)

# remove one variable label
dummy <- set_label(dummy, c("Variable A", "", "Variable C"))
str(dummy)


# 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)


# use 'var_labels()' to set labels within a pipe-workflow, and
# when you need "tidyverse-consistent" api.
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