When called with named arguments, sets the label attribute on the
specified columns and returns the modified data frame. When called without
arguments, returns a named character vector of current labels.
Value
When setting: the data frame with updated label attributes (invisibly pipeable). When getting: a named character vector of labels.
See also
Other metadata:
get_metadata(),
set_dataset_label(),
set_format(),
set_length()
Examples
df <- data.frame(STUDYID = "S1", AGE = 65, stringsAsFactors = FALSE)
# Set labels
df <- set_label(df, STUDYID = "Study Identifier", AGE = "Age in Years")
attr(df$STUDYID, "label")
#> [1] "Study Identifier"
# Get labels
set_label(df)
#> STUDYID AGE
#> "Study Identifier" "Age in Years"
# Pipeline
df2 <- data.frame(X = 1, Y = 2) |>
set_label(X = "Variable X", Y = "Variable Y") |>
set_format(X = "8.1")
# Tidy eval: splice a named list with !!!
labels <- list(STUDYID = "Study Identifier", AGE = "Age")
df3 <- set_label(df, !!!labels)
# Tidy eval: dynamic column name with := and !!
col <- "STUDYID"
df4 <- set_label(df, !!col := "Study ID")
