The functions lama_translate_all() and lama_to_factor_all() converts all variables (which have a translation in the given lama-dictionary) of a data frame .data into factor variables with new labels. These functions are special versions of the functions lama_translate() and lama_to_factor(). The difference to lama_translate() and lama_to_factor() is, that when using lama_translate_all() and lama_to_factor_all() the used translations in dictionary must have the exact same names as the corresponding columns in the data frame .data.

lama_translate_all(.data, dictionary, prefix = "", suffix = "",
  fn_colname = function(x) x, keep_order = FALSE, to_factor = TRUE)

# S3 method for data.frame
lama_translate_all(.data, dictionary, prefix = "",
  suffix = "", fn_colname = function(x) x, keep_order = FALSE,
  to_factor = TRUE)

lama_to_factor_all(.data, dictionary, prefix = "", suffix = "",
  fn_colname = function(x) x, keep_order = FALSE)

# S3 method for data.frame
lama_to_factor_all(.data, dictionary, prefix = "",
  suffix = "", fn_colname = function(x) x, keep_order = FALSE)

Arguments

.data

Either a data frame, a factor or a vector.

dictionary

A lama_dictionary object, holding the translations for various variables.

prefix

A character string, which is used as prefix for the new column names.

suffix

A character string, which is used as suffix for the new column names.

fn_colname

A function, which transforms character string into a new character string. This function will be used to transform the old column names into new column names under which the labeled variables will then be stored.

keep_order

A logical of length one, defining if the original order (factor order or alphanumerical order) of the data frame variables should be preserved.

to_factor

A logical of length one, defining if the resulting labeled variables should be factor variables (to_factor = TRUE) or plain character vectors (to_factor = FALSE).

Value

An extended data.frame, that has a factor variable holding the assigned labels.

Details

The difference between lama_translate_all() and lama_to_factor_all() is the following:

  • lama_translate_all(): Assign new labels to the variables and turn them into factor variables with the order given in the corresponding translations (keep_order = FALSE) or in the same order as the original variable (keep_order = TRUE).

  • lama_to_factor_all(): The variables are character vectors or factors already holding the right label strings. The variables are turned into a factor variables with the order given in the corresponding translation (keep_order = FALSE) or in the same order as the original variable (keep_order = TRUE).

See also

lama_translate(), lama_to_factor(), new_lama_dictionary(), as.lama_dictionary(), lama_rename(), lama_select(), lama_mutate(), lama_merge(), lama_read(), lama_write()

Examples

## initialize lama_dictinoary dict <- new_lama_dictionary( subject = c(en = "English", ma = "Mathematics"), result = c("1" = "Very good", "2" = "Good", "3" = "Not so good") ) ## data frame which should be translated df <- data.frame( pupil = c(1, 1, 2, 2, 3), subject = c("en", "ma", "ma", "en", "en"), result = c(1, 2, 3, 2, 2) ) ## Example-1: 'lama_translate_all'' df_new <- lama_translate_all( df, dict, prefix = "pre_", fn_colname = toupper, suffix = "_suf" ) str(df_new)
#> 'data.frame': 5 obs. of 5 variables: #> $ pupil : num 1 1 2 2 3 #> $ subject : Factor w/ 2 levels "en","ma": 1 2 2 1 1 #> $ result : num 1 2 3 2 2 #> $ pre_SUBJECT_suf: Factor w/ 2 levels "English","Mathematics": 1 2 2 1 1 #> $ pre_RESULT_suf : Factor w/ 3 levels "Very good","Good",..: 1 2 3 2 2
## Example-2: 'lama_translate_all' with 'to_factor = FALSE' # The resulting variables are plain character vectors df_new <- lama_translate_all(df, dict, suffix = "_new", to_factor = TRUE) str(df_new)
#> 'data.frame': 5 obs. of 5 variables: #> $ pupil : num 1 1 2 2 3 #> $ subject : Factor w/ 2 levels "en","ma": 1 2 2 1 1 #> $ result : num 1 2 3 2 2 #> $ subject_new: Factor w/ 2 levels "English","Mathematics": 1 2 2 1 1 #> $ result_new : Factor w/ 3 levels "Very good","Good",..: 1 2 3 2 2
## Example-3: 'lama_to_factor_all' # The variables 'subject' and 'result' are turned into factor variables # The ordering is taken from the translations 'subject' and 'result' df_2 <- data.frame( pupil = c(1, 1, 2, 2, 3), subject = c("English", "Mathematics", "Mathematics", "English", "English"), result = c("Very good", "Good", "Good", "Very good", "Good") ) df_2_new <- lama_to_factor_all( df_2, dict, prefix = "pre_", fn_colname = toupper, suffix = "_suf" ) str(df_new)
#> 'data.frame': 5 obs. of 5 variables: #> $ pupil : num 1 1 2 2 3 #> $ subject : Factor w/ 2 levels "en","ma": 1 2 2 1 1 #> $ result : num 1 2 3 2 2 #> $ subject_new: Factor w/ 2 levels "English","Mathematics": 1 2 2 1 1 #> $ result_new : Factor w/ 3 levels "Very good","Good",..: 1 2 3 2 2