Skip to contents

Format factor levels as arbitrary values of Yes/No (with Yes always first) while leaving untouched all vectors that contain other information. Default level values can be set through options, which is best done before calling read_trialmaster(). Helper get_yesno_lvl() is a helper to provide default TM levels.

Usage

fct_yesno(
  x,
  lvl = getOption("edc_fct_yesno", get_yesno_lvl()),
  mutate_character = TRUE
)

get_yesno_lvl(add, keep_default = TRUE)

Arguments

x

a vector of any type/class

lvl

list of values to be considered as Yes/No values. Defaults to get_yesno_lvl(). See example.

mutate_character

whether to turn characters into factor

add

levels to add to list(c("Yes", "No"), c("1-Yes", "0-No"))

keep_default

whether to keep the default

Value

a factor, or x untouched

Examples

set.seed(42)
x = tibble(a=sample(c("Yes", "No"), size=20, replace=TRUE),
           b=sample(c("1-Yes", "0-No"), size=20, replace=TRUE),
           c=sample(c("Oui", "Non"), size=20, replace=TRUE),
           x=sample(0:1, size=20, replace=TRUE),
           y=1:20)

# leave untouched unhandled vectors (c,x, and y)
x %>% purrr::iwalk(~{
  cat("--- Levels of ", .y, " ---\n")
  print(.x %>% factor() %>% levels())
  print(.x %>% fct_yesno() %>% levels())
})
#> --- Levels of  a  ---
#> [1] "No"  "Yes"
#> [1] "Yes" "No" 
#> --- Levels of  b  ---
#> [1] "0-No"  "1-Yes"
#> [1] "1-Yes" "0-No" 
#> --- Levels of  c  ---
#> [1] "Non" "Oui"
#> NULL
#> --- Levels of  x  ---
#> [1] "0" "1"
#> [1] "Yes" "No" 
#> --- Levels of  y  ---
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
#> [16] "16" "17" "18" "19" "20"
#> NULL

#add other levels
supp_levels = list(c("Oui", "Non"), c("Ja", "Nein"))
options(edc_fct_yesno = get_yesno_lvl(supp_levels))
x %>% purrr::iwalk(~{
  cat("--- Levels of ", .y, " ---\n")
  print(.x %>% factor() %>% levels())
  print(.x %>% fct_yesno() %>% levels())
})
#> --- Levels of  a  ---
#> [1] "No"  "Yes"
#> [1] "Yes" "No" 
#> --- Levels of  b  ---
#> [1] "0-No"  "1-Yes"
#> [1] "1-Yes" "0-No" 
#> --- Levels of  c  ---
#> [1] "Non" "Oui"
#> [1] "Oui" "Non"
#> --- Levels of  x  ---
#> [1] "0" "1"
#> [1] "Yes" "No" 
#> --- Levels of  y  ---
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
#> [16] "16" "17" "18" "19" "20"
#> NULL