3 Labelled data

One of the best (and somewhat accidental) innovations of the haven package is the introduction of value labels and other metadata tags that we commonly see when working with other statistical software into R, primarily via the labelled vector.

Labelled vectors were created as an R equivalent to categorical-esque types. Originally this was only intended as a pass-through class to get to factors. As the labelled() documentation from haven says:

This class provides few methods, as I expect you’ll coerce to a standard R class (e.g. a factor()) soon after importing.

It turns out that the labelled class is immensely useful in its own right. Fortunately R lives in the open source world, and the labelled package was created. This provides a set of helper functions for more easily working with labelled datasets, particularly for label editing and manipulation.

We’ll be going through some brief examples of working with labels, but for a more detailed general introduction see the Introduction to labelled vignette. The labelled cheat sheet is a fantastic quick function reference.