The labels function extracts all assigned labels from a data frame, and returns them in a named list. The function also assigns labels from a named list. This function is a data frame-specific implementation of the Base R labels function.

# S3 method for data.frame
labels(object, ...)

labels(x) <- value

Arguments

object

A data frame or tibble.

...

Follow-on parameters. Required for generic function.

x

A data frame or tibble

value

A named list of labels The labels must be quoted strings.

Value

A named list of labels. The labels must be quoted strings.

Details

If labels are assigned to the "label" attributes of the data frame columns, the labels function will extract those labels. The function will return the labels in a named list, where the names correspond to the name of the column that the label was assigned to. If a column does not have a label attribute assigned, that column will not be included in the list.

When used on the receiving side of an assignment, the function will assign labels to a data frame. The labels should be in a named list, where each name corresponds to the data frame column to assign the label to.

See also

fdata to display formatted data, value to create user-defined formats, and fapply to apply formats to a vector.

Examples

# Take subset of data
df1 <- mtcars[1:10, c("mpg", "cyl") ]

# Assign labels
labels(df1) <- list(mpg = "Mile Per Gallon", cyl = "Cylinders") 

# Examine attributes
str(df1)

# View assigned labels
labels(df1)