The creation of lama-dictionaries was described in Creating lama-dictionaries and in Translating variables we saw how to use them in order to assign the right labels to our data.

Now we will see, how to effectively alter lama-dictionaries, so that we get dictionaries holding the right translations. labelmachine contains a light weight frame work for altering lama-dictionaries, similar to the package dplyr:

The commands which have no underscore at the end of the command name (lama_rename(), lama_select(), lama_mutate()) use non-standard evaluation. This means, that instead of passing in translation names as strings (e.g. lama_rename_(dict, "old", "new")), we can pass in unquoted expressions (e.g. lama_rename(dict, new = old)), which are automatically parsed. Often non-standard evaluation saves some time in writing, but sometimes we want to pass in the names as character vectors. In this case, we need to use the standard evaluation variants of the commands. These commands have the same names, but end on a underscore (e.g. lama_rename_(), lama_select_(), lama_mutate_()).

In the following part we will alter the following dictionary:

Select a subset of translations

Sometimes we want to keep a subset of translations, in this case we can use lama_select() and lama_select_(). With lama_select() we can use unquoted translation names:

The resulting dictionary dict_new now only contains the translations sub and lev.

With lama_select_() we pass in a single character vector, which holds the names of the translations we want to keep:

Alter translations

The commands lama_mutate() and lama_mutate_() are used to alter or delete existing translations in a lama-dictionary or add new translations (named character vectors) to it. With lama_mutate() we use unquoted expressions:

Besides the argument .data all other arguments are translation assignment and the given argument names are used as the names to which the translations, given on the right hand side of the equation, will be assigned:

  • The translation c(jane = "Jane Doe", john = "John Doe") is assigned to the translation name teacher.
  • In the expression given for translation sub, uses the object name sub inside of the expression (e.g. c(geo = "Geography", sub)) and is evaluated in this way. Therefore, the resulting translation sub is the combination of the label assignment geo = "Geographry" and the label assignments given in the old translation sub (e.g. c(eng = "English", mat = "Mathematics", gym = "Gymnastics")).
  • The assignment lev = NULL deletes the translation with the name lev.
  • The assignment result = c(P = "Passed", F = "Failed") overwrites the translation result with a new translation.

The command lama_mutate_() is uses standard evaluation and can only alter one translation at a time. We pass in a character string holding the name of the translation we want to alter and a second argument holding the translation (named character vector), we want to assign:

Merging lama-dictionaries

With the command lama_merge we can merge two or more lama-dictionaries together into a single lama-dictionary.

Let us consider the following dictionaries:

dict_a <- new_lama_dictionary(a = c(a = "A"), x = c(x = "A"), y = c(y = "A"))
dict_b <- new_lama_dictionary(b = c(b = "B"), x = c(x = "B"), z = c(z = "B"))
dict_c <- new_lama_dictionary(c = c(c = "C"), z = c(x = "B"))

We merge them together into a new dictionary:

The merging is done from left to right. This means that the lama-dictionary dict_a is partially overwritten by dict_b and the resulting lama-dictionary is then partially overwritten by dict_c.