R/wfwl.R

Defines functions wl wf

Documented in wf wl

#' @title Get the index of the first/last TRUE
#'
#' @description
#' A quick C implementation for \dQuote{which.first} (\code{head(which(x), 1)}) and
#' \dQuote{which.last} (\code{tail(which(x), 1)}).
#'
#' @param x [\code{logical}]\cr
#'  Logical vector.
#' @param use.names [\code{logical(1)}]\cr
#'  If \code{TRUE} and \code{x} is named, the result is also
#'  named.
#' @return [\code{integer(1)} | \code{integer(0)}].
#'  Returns the index of the first/last \code{TRUE} value in \code{x} or
#'  an empty integer vector if none is found. NAs are ignored.
#' @useDynLib checkmate c_which_first
#' @export
#' @examples
#' wf(c(FALSE, TRUE))
#' wl(c(FALSE, FALSE))
#' wf(NA)
wf = function(x, use.names = TRUE) {
  .Call(c_which_first, x, use.names)
}

#' @rdname wf
#' @export
#' @useDynLib checkmate c_which_last
wl = function(x, use.names = TRUE) {
  .Call(c_which_last, x, use.names)
}

Try the checkmate package in your browser

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

checkmate documentation built on Sept. 11, 2024, 8:40 p.m.