View source: R/apply_data_labels.R
apply_data_labels | R Documentation |
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.
apply_data_labels(data, vars, labels)
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
|
A labelled data.frame or a tbl_df.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.