R/which_firstNA.R

Defines functions which_lastNA which_firstNA

Documented in which_firstNA which_lastNA

#' First/last position of missing values
#' @description Introduced in \code{v 1.6.0}
#'
#' @param x An atomic vector.
#' @return The position of the first/last missing value in \code{x}.
#'
#' @examples
#' N <- 1e8
#' N <- 1e6  # for CRAN etc
#' x <- c(1:1e5, NA, integer(N))
#' bench_system_time(which.max(is.na(x))) # 123ms
#' bench_system_time(Position(is.na, x))  #  22ms
#' bench_system_time(which_firstNA(x))    #  <1ms

#' @export
which_firstNA <- function(x) {
  if (is.null(x)) {
    return(0L)
  }
  stopifnot(is.atomic(x))
  return(R_xlen_t(do_which_firstNA(x)))
}

#' @rdname which_firstNA
#' @export
which_lastNA <- function(x) {
  if (is.null(x)) {
    return(0L)
  }
  stopifnot(is.atomic(x))
  return(R_xlen_t(do_which_lastNA(x)))
}

Try the hutilscpp package in your browser

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

hutilscpp documentation built on Oct. 11, 2023, 9:06 a.m.