apply_data_labels: Set variable labels

View source: R/apply_data_labels.R

apply_data_labelsR Documentation

Set variable labels

Description

Set variable labels for a data frame or tibble. Basically a wrapper around labelled::var_label() with a syntax that I can remember and some additional warnings and fixes that I like to have.

If a label is not given, then the variable name in the data is used as the label.

Usage

apply_data_labels(data, vars, labels)

Arguments

data

A data.frame or tbl_df

vars

A character vector of variable names.

labels

A character vector of labels. Must be the same length as vars.

Value

A labelled data.frame or a tbl_df.

Examples

library(labelled)
# Load some packages for the example
library(survival)
library(dplyr)
library(tibble)

# Data set comes from the survival package
data(pbc)

# Tibble with labels
var_labels <- tibble::tribble(
  ~vars,                                      ~labels,
  "id",                                "Case Number",
  "time",          "Number of days since registration",
  "status",                         "Status at endpoint",
  "trt",                            "Treatment group",
  "age",                              "Age, in years",
  "sex",                                        "Sex",
  "ascites",                        "Presence of ascites",
  "hepato", "Presence of hepatomegaly or enlarged liver",
  "spiders",     "Blood vessel malformations in the skin",
  "edema",                          "Presence of edema",
  "bili",                   "Serum bilirunbin (mg/dl)",
  "chol",                  "Serum cholesterol (mg/dl)",
  "albumin",                       "Serum albumin (g/dl)",
  "copper",                      "Urine copper (ug/day)",
  "alk.phos",             "Alkaline phosphotase (U/liter)",
  "ast",          "Aspartate aminotransferase (U/ml)",
  "trig",                      "Triglycerides (mg/dl)",
  "platelet",                             "Platelet count",
  "protime",           "Standardised blood clotting time",
  "stage", "Histologic stage of disease (needs biopsy)"
)


pbc <- apply_data_labels(data = pbc,
                         vars = var_labels$vars,
                         labels = var_labels$labels)
labelled::var_label(pbc)
str(pbc)

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.