R/to_na.R

Defines functions val_labels_to_na.data.frame val_labels_to_na.haven_labelled val_labels_to_na.default val_labels_to_na nolabel_to_na.data.frame nolabel_to_na.haven_labelled nolabel_to_na.default nolabel_to_na

Documented in nolabel_to_na val_labels_to_na

#' Recode values with no label to NA
#'
#' For labelled variables, values with no label will be recoded to `NA`.
#'
#' @param x Object to recode.
#' @examples
#' v <- labelled(c(1, 2, 9, 1, 9), c(yes = 1, no = 2))
#' nolabel_to_na(v)
#' @export
nolabel_to_na <- function(x) {
  UseMethod("nolabel_to_na")
}

#' @export
nolabel_to_na.default <- function(x) {
  x
}

#' @export
nolabel_to_na.haven_labelled <- function(x) {
  allval <- unique(x)
  allval <- allval[!is.na(allval)]
  nolabel <- allval[!allval %in% val_labels(x)]
  if (length(nolabel) > 0)
    x[x %in% nolabel] <- NA
  x
}

#' @export
nolabel_to_na.data.frame <- function(x) {
  x[] <- lapply(x, nolabel_to_na)
  x
}

#' Recode value labels  to NA
#'
#' For labelled variables, values with a label will be recoded to `NA`.
#'
#' @param x Object to recode.
#' @seealso [haven::zap_labels()]
#' @examples
#' v <- labelled(c(1, 2, 9, 1, 9), c(dk = 9))
#' val_labels_to_na(v)
#' @export
val_labels_to_na <- function(x) {
  UseMethod("val_labels_to_na")
}

#' @export
val_labels_to_na.default <- function(x) {
  x
}

#' @export
val_labels_to_na.haven_labelled <- function(x) {
  val <- val_labels(x)
  if (length(val) > 0)
    x[x %in% val] <- NA
  val_labels(x) <- NULL
  x
}

#' @export
val_labels_to_na.data.frame <- function(x) {
  x[] <- lapply(x, val_labels_to_na)
  x
}

Try the labelled package in your browser

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

labelled documentation built on July 9, 2023, 7:53 p.m.