# NOT RUN {
# initialize lama_dictinoary
dict <- new_lama_dictionary(
subject = c(en = "English", ma = "Mathematics"),
result = c("1" = "Very good", "2" = "Good", "3" = "Not so good")
)
# the data frame which should be translated
df <- data.frame(
pupil = c(1, 1, 2, 2, 3),
subject = c("en", "ma", "ma", "en", "en"),
res = c(1, 2, 3, 2, 2)
)
## Example-1: Usage of 'lama_translate' for data frames
## Full length assignment
# (apply translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply translation 'result' to column 'res' and save it to column 'res_new')
df_new <- lama_translate(
df,
dict,
sub_new = subject(subject),
res_new = result(res)
)
str(df_new)
## Example-2: Usage of 'lama_translate' for data frames
## Abbreviation overwriting original columns
# (apply translation 'subject' to column 'subject' and save it to column 'subject')
# (apply translation 'result' to column 'res' and save it to column 'res')
df_new_overwritten <- lama_translate(
df,
dict,
subject(subject),
result(res)
)
str(df_new_overwritten)
## Example-3: Usage of 'lama_translate' for data frames
## Abbreviation if `translation_name == column_name`
# (apply translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply translation 'result' to column 'res' and save it to column 'res_new')
df_new_overwritten <- lama_translate(
df,
dict,
subject_new = subject,
res_new = result(res)
)
str(df_new_overwritten)
## Example-4: Usage of 'lama_translate' for data frames labeling as character vectors
# (apply translation 'subject' to column 'subject' and
# save it as a character vector to column 'subject_new')
df_new_overwritten <- lama_translate(
df,
dict,
subject_new = subject,
to_factor = TRUE
)
str(df_new_overwritten)
## Example-5: Usage of 'lama_translate' for atomic vectors
sub <- c("ma", "en", "ma")
sub_new <- df_new_overwritten <- lama_translate(
sub,
dict,
subject
)
str(sub_new)
## Example-6: Usage of 'lama_translate' for factors
sub <- factor(c("ma", "en", "ma"), levels = c("ma", "en"))
sub_new <- df_new_overwritten <- lama_translate(
sub,
dict,
subject,
keep_order = TRUE
)
str(sub_new)
## Example-7: Usage of 'lama_translate_' for data frames
# (apply translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply translation 'result' to column 'res' and save it to column 'res_new')
df_new <- lama_translate_(
df,
dict,
translation = c("subject", "result"),
col = c("subject", "res"),
col_new = c("subject_new", "res_new")
)
str(df_new)
## Example-8: Usage of 'lama_translate_' for data frames and store as character vector
# (apply translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply translation 'result' to column 'res' and save it to column 'res_new')
df_new <- lama_translate_(
df,
dict,
translation = c("subject", "result"),
col = c("subject", "res"),
col_new = c("subject_new", "res_new"),
to_factor = c(FALSE, FALSE)
)
str(df_new)
## Example-9: Usage of 'lama_translate_' for atomic vectors
res <- c(1, 2, 1, 3, 1, 2)
res_new <- df_new_overwritten <- lama_translate_(
res,
dict,
"result"
)
str(res_new)
## Example-10: Usage of 'lama_translate_' for factors
sub <- factor(c("ma", "en", "ma"), levels = c("ma", "en"))
sub_new <- df_new_overwritten <- lama_translate_(
sub,
dict,
"subject",
keep_order = TRUE
)
str(sub_new)
# the data frame which holds the right labels, but no factors
df_translated <- data.frame(
pupil = c(1, 1, 2, 2, 3),
subject = c("English", "Mathematics", "Mathematics", "English", "English"),
res = c("Very good", "Good", "Not so good", "Good", "Good")
)
## Example-11: Usage of 'lama_to_factor' for data frames
## Full length assignment
# (apply order of translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply order of translation 'result' to column 'res' and save it to column 'res_new')
df_new <- lama_to_factor(
df_translated,
dict,
sub_new = subject(subject),
res_new = result(res)
)
str(df_new)
## Example-12: Usage of 'lama_to_factor' for data frames
## Abbreviation overwriting original columns
# (apply order of translation 'subject' to column 'subject' and save it to column 'subject')
# (apply order of translation 'result' to column 'res' and save it to column 'res')
df_new_overwritten <- lama_to_factor(
df_translated,
dict,
subject(subject),
result(res)
)
str(df_new_overwritten)
## Example-13: Usage of 'lama_to_factor' for data frames
## Abbreviation if `translation_name == column_name`
# (apply order of translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply order of translation 'result' to column 'res' and save it to column 'res_new')
df_new_overwritten <- lama_to_factor(
df_translated,
dict,
subject_new = subject,
res_new = result(res)
)
str(df_new_overwritten)
## Example-14: Usage of 'lama_translate' for atomic vectors
var <- c("Mathematics", "English", "Mathematics")
var_new <- lama_to_factor(
var,
dict,
subject
)
str(var_new)
## Example-15: Usage of 'lama_to_factor_' for data frames
# (apply order of translation 'subject' to column 'subject' and save it to column 'subject_new')
# (apply order of translation 'result' to column 'res' and save it to column 'res_new')
df_new <- lama_to_factor_(
df_translated,
dict,
translation = c("subject", "result"),
col = c("subject", "res"),
col_new = c("subject_new", "res_new")
)
str(df_new)
## Example-16: Usage of 'lama_to_factor_' for atomic vectors
var <- c("Very good", "Good", "Good")
var_new <- lama_to_factor_(
var,
dict,
"result"
)
str(var_new)
# }
Run the code above in your browser using DataLab