labelmachine is an R package that helps assigning meaningful labels to R data sets. Manage your labels in yaml files, so called lama-dictionary files. This makes it very easy using the same label translations in multiple projects that share similar data structure.

Labeling your data can be easy!

The example data frame

Let us assume, you want to label the following data frame df:

The column subject contains the subject codes the pupils were tested in and result contains the test results.

Your first lama-dictionary

In order to assign labels to the values in the columns of df, we need a lama-dictionary which holds the translations of the variables. With the command new_lama_dictionary() we can create such a lama-dictionary:

Each entry in dict is a translation for a variable (column) of the data frame df. The translation sub can be used to assign labels to the values given in column subject in df and translation res can be used to assign labels to the values in column result in df. The expression NA_ is used to escape the missing value symbol NA. Hence, the last assignment NA_ = "Missed" defines that missing values should be labeled with the string "Missed". For further details on creating lama-dictionaries see Creating lama-dictionaries and Altering lama-dictionaries.

Translate the data frame columns

With the command lama_translate, we can use the lama-dictionary dict in order to translate the variables given in data frame df:

As we can see, the resulting data frame df_new now holds two extra columns subject_lab and result_lab holding the factor variables with the right labels. The command lama_translate has multiple features, which are described in more detail in Translating variables.

Save your lama-dictionary

With the command lama_write it is possible to save the lama-dictionary object to a yaml file:

path_to_file <- file.path(tempdir(), "my_dictionary.yaml")
lama_write(dict, path_to_file)

The resulting yaml file is a plain text file with a special text structure, see dictionary.yaml.

Lama-dictionary files make it easy to share lama-dictionaries with other projects holding similar data structures. With the command lama_read a lama-dictionary file can be read:

path_to_file <- system.file("extdata", "dictionary_exams.yaml", package = "labelmachine")
dict <- lama_read(path_to_file)