Learn R Programming

cheapr (version 1.1.0)

which_: Memory-efficient alternative to which()

Description

Exactly the same as which() but more memory efficient.

Usage

which_(x, invert = FALSE)

Value

An unnamed integer vector.

Arguments

x

A logical vector.

invert

If TRUE, indices of values that are not TRUE are returned (including NA). If FALSE (the default), only TRUE indices are returned.

Details

This implementation is similar in speed to which() but usually more memory efficient.

Examples

Run this code
library(cheapr)
library(bench)
x <- sample(c(TRUE, FALSE), 1e05, TRUE)
x[sample.int(1e05, round(1e05/3))] <- NA

mark(which_(TRUE), which(TRUE))
mark(which_(FALSE), which(FALSE))
mark(which_(logical()), which(logical()))
mark(which_(x), which(x), iterations = 20)
mark(base = which(is.na(match(x, TRUE))),
     collapse = collapse::whichv(x, TRUE, invert = TRUE),
     cheapr = which_(x, invert = TRUE),
     iterations = 20)

Run the code above in your browser using DataLab