Learn R Programming

R.utils (version 1.9.7)

whichVector.logical: Identifies TRUE elements in a logical vector

Description

Identifies TRUE elements in a logical vector. This method is faster than which() for logical vectors, especially when there are no missing values.

Usage

## S3 method for class 'logical':
whichVector(x, na.rm=TRUE, use.names=TRUE, ...)

Arguments

x
A logical vector of length N.
na.rm
If TRUE, missing values are treated as FALSE, otherwise they are returned as NA.
use.names
If TRUE, the names attribute is preserved, otherwise it is not return.
...
Not used.

Value

Benchmarking

Simple comparison on R v2.7.1 on Windows XP, show that this implementation can be more than twice as fast as which(), especially when there are no missing value (and na.rm=FALSE) is used.

See Also

which()

Examples

Run this code
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Simulate two large named logical vectors,
# one with missing values one with out
# - - - - - - - - - - - - - - - - - - - - - - - - - -
N <- 1e6;

# Vector #1
x <- sample(c(TRUE, FALSE), size=N, replace=TRUE);
names(x) <- seq_along(x);

# Vector #2
y <- x
y[sample(N, size=0.1*N)] <- NA;


# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Validate consistency
# - - - - - - - - - - - - - - - - - - - - - - - - - -
stopifnot(identical(which(x), whichVector(x)));
stopifnot(identical(which(y), whichVector(y)));


# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Benchmarking
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Number of iterations
K <- 5;

t1 <- 0;
for (kk in 1:K) {
  t1 <- t1 + system.time({ idxs1 <- which(x) });
};
 
t2 <- 0;
for (kk in 1:K) {
  t2 <- t2 + system.time({ idxs2 <- whichVector(x, na.rm=FALSE) });
};
 
cat(sprintf("whichVector(x, na.rm=FALSE)/which(x): %.2f
", (t2/t1)[3]));
stopifnot(identical(idxs1, idxs2));


t1 <- 0;
for (kk in 1:K) {
  t1 <- t1 + system.time({ idxs1 <- which(y) });
};
 
t2 <- 0;
for (kk in 1:K) {
  t2 <- t2 + system.time({ idxs2 <- whichVector(y) });
};
 
cat(sprintf("whichVector(y)/which(y): %.2f
", (t2/t1)[3]));
stopifnot(identical(idxs1, idxs2));

Run the code above in your browser using DataLab