Skip to contents

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.

Usage

set_label(data, ...)

Arguments

data

A data frame.

...

Named arguments where names are column names and values are label strings. Supports tidy evaluation: use !! to inject a single name, !!! to splice a named list, and := with glue syntax for dynamic names. If empty, returns current labels.

Value

When setting: the data frame with updated label attributes (invisibly pipeable). When getting: a named character vector of labels.

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