lama_translate_all: Assign new labels to all variables of a data.frame

Description Usage Arguments Details Value See Also Examples

View source: R/lama_translate_all.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
lama_translate_all(.data, dictionary, prefix = "", suffix = "",
  fn_colname = function(x) x, keep_order = FALSE, to_factor = TRUE)

## S3 method for class '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 class '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).

Details

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

Value

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  ## 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)

  ## 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)

  ## 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)

labelmachine documentation built on Oct. 11, 2019, 9:05 a.m.