Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make_yes_* need to keep labels #50

Open
RaymondBalise opened this issue Mar 1, 2023 · 0 comments
Open

make_yes_* need to keep labels #50

RaymondBalise opened this issue Mar 1, 2023 · 0 comments

Comments

@RaymondBalise
Copy link
Owner

make_yes_no() and make_yes_no_unknown() functions currently throw away the labels. They need to be changed to be something like this:

make_yes_no_unknown <- function(x) {
  if (is.factor(x) | is.character(x)) {
    recoded <- 
    factor(dplyr::case_when(
      str_detect(x, stringr::regex("^yes", ignore_case = TRUE)) == TRUE ~ "Yes", 
      str_detect(x, stringr::regex("^checked", ignore_case = TRUE)) == TRUE ~ "Yes", 
      str_detect(x, stringr::regex("^no", ignore_case = TRUE)) == TRUE ~ "No", 
      str_detect(x, stringr::regex("^unchecked", ignore_case = TRUE)) == TRUE ~ "No", 
      TRUE ~ "Unknown"), levels = c("No", "Yes", "Unknown"))
  }
  else if (is.numeric(x) | is.logical(x)) {
    recoded <- 
    factor(dplyr::case_when(x == 1 ~ "Yes", x == 0 ~ "No", 
                            TRUE ~ "Unknown"), 
           levels = c("No", "Yes", "Unknown"))
  }
  else {
    recoded <- 
    x
  }
  
  if(labelVector::is_labelled(x)){
    labelVector::set_label(recoded, labelVector::get_label(x))
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant