# whichVector.logical

0th

Percentile

##### Identifies TRUE elements in a logical vector

Identifies TRUE elements in a logical vector.

NOTE: which() should be used instead of this method unless you are running R (< 2.11.0), for which this method is faster than which() for logical vectors, especially when there are no missing values.

Keywords
methods, internal, programming
##### Usage
# S3 method for 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

Returns an integer vector of length less or equal to N.

##### Benchmarking

In R v2.11.0 which() was made approx. 10 times faster via a native implementation. Because of this, this method is of little use and approximately 3 times slower. However, for earlier version of R, this method is still significantly faster. For example, 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.

which()

##### Aliases
• whichVector.logical
• whichVector.matrix
##### Examples
# NOT RUN {
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Simulate two large named logical vectors,
# one with missing values one without
# - - - - - - - - - - - - - - - - - - - - - - - - - -
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\n", (t2/t1)));
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\n", (t2/t1)));
stopifnot(identical(idxs1, idxs2));

# }

Documentation reproduced from package R.utils, version 2.8.0, License: LGPL (>= 2.1)

### Community examples

Looks like there are no examples yet.