R/to_na.R

Defines functions to_na

Documented in to_na

#' @title Replace NaN and Inf with NA
#'
#' @description
#' Takes out \code{NaN} and \code{Inf} and replaces them with \code{NA}
#'
#' @param x a vector
#'
#' @return Returns vector with replaced \code{NA} values.
#' @export
#'
#' @examples
#' test <- list(a = c("a", "b", NA),
#'              b = c(NaN, 1,2, -Inf),
#'              c = c(TRUE, FALSE, NA))
#'
#' lapply(test, to_na)
#'
#' ## Output
#' # $a
#' # [1] "a" "b" NA
#'
#' # $b
#' # [1] NA  1  2 NA
#'
#' # $c
#' # [1] TRUE FALSE NA
#'
#' @author Daniel Luettgau
#'

to_na <- function(x) {
  # check input
  if (!is.vector(x)) {
    stop("input must be a vector")
  }

  # convert input
  if (is.character(x)) {
    return(x)
  } else {
    ifelse(is.infinite(x) | is.nan(x), NA, x)
  }
}
STATWORX/helfRlein documentation built on Feb. 12, 2024, 2:21 a.m.