R/util_as_valid_missing_codes.R

Defines functions util_as_valid_missing_codes

Documented in util_as_valid_missing_codes

#' Convert `x` to valid missing codes
#'
#' @param x [character] a vector of values
#'
#' @return converted `x`
#' @family robustness_functions
#' @keywords internal
 util_as_valid_missing_codes <- function(x) {
   # TODO: Use it everywhere, where we still have literally defined conversions, currently.
   r <- lapply(x, as.character)
  which_numeric <-  suppressWarnings(is.na(r) == is.na(as.numeric(r)))
  which_datetime <-  suppressWarnings(is.na(r) ==
                                       is.na(lapply(r, lubridate::as_datetime)))
  which_numeric <- which_numeric & !which_datetime
  r[which_numeric] <- as.numeric(r[which_numeric])
  r[which_datetime] <- lapply(x[which_datetime], lubridate::as_datetime)
  r[!which_numeric & !which_datetime] <- NA_character_
  r
}

Try the dataquieR package in your browser

Any scripts or data that you put into this service are public.

dataquieR documentation built on May 29, 2024, 7:18 a.m.