Intro
Defining variable labels is a useful way to describe and document datasets. Unlike SPSS, which makes it very easy to define variable labels using the data editor, base R doesn't provide any function to define variable labels (as far as I know).
However, Daniel Luedecke's R package sjlablled
fills this gap. Let's give an example.
Defining variable labels
First, we load the mtcars
data frame and define variable labels for all of the 11 variables:
data(mtcars) labs <- c("Miles/(US) gallon", "Number of cylinders", "Displacement (cu.in.)", "Gross horsepower", "Rear axle ratio", "Weight (1000 lbs)", "1/4 mile time", "V/S", "Transmission", "Number of forward gears", "Number of carburetors")
Assigning labels to variables
Second, we assign the variable labels to the variables of the mtcars
data frame:
library(sjlabelled) mtcars <- set_label(mtcars, label = labs)
When we have a look at the mtcars
data frame using RStudio's data viewer, we find the variable labels placed right underneath the variable names:
Moreover, we may as well save both variable names and labels into a data frame:
library(dplyr) # for data manipulation library(knitr) # for printing tables df <- get_label(mtcars) %>% data.frame() %>% rename_at(vars(1), funs(paste0('var.labs'))) %>% mutate(var.names = colnames(mtcars)) kable(df, align = 'lc')
var.labs | var.names |
---|---|
Miles/(US) gallon | mpg |
Number of cylinders | cyl |
Displacement (cu.in.) | disp |
Gross horsepower | hp |
Rear axle ratio | drat |
Weight (1000 lbs) | wt |
¼ mile time | qsec |
V/S | vs |
Transmission | am |
Number of forward gears | gear |
Number of carburetors | carb |