\HeaderA{which}{Which indices are TRUE?}{which}
\keyword{logic}{which}
\keyword{attribute}{which}
\begin{Description}\relax
Give the \code{TRUE} indices of a logical object, allowing for array
indices.
\end{Description}
\begin{Usage}
\begin{verbatim}
which(x, arr.ind = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a \code{\LinkA{logical}{logical}} vector or array.  \code{\LinkA{NA}{NA}}s
are allowed and omitted (treated as if \code{FALSE}).
\item[\code{arr.ind}] logical; should \bold{arr}ay \bold{ind}ices be returned
when \code{x} is an array?
\end{ldescription}
\end{Arguments}
\begin{Value}
If \code{arr.ind == FALSE} (the default), an integer vector with
\code{length} equal to \code{sum(x)}, i.e., to the number of
\code{TRUE}s in \code{x}; Basically, the result is
\code{(1:length(x))[x]}.

If \code{arr.ind == TRUE} and \code{x} is an \code{\LinkA{array}{array}} (has
a \code{\LinkA{dim}{dim}} attribute), the result is a matrix whose rows each
are the indices of one element of \code{x}; see Examples below.
\end{Value}
\begin{Author}\relax
Werner Stahel and Peter Holzer \email{holzer@stat.math.ethz.ch},
for the array case.
\end{Author}
\begin{SeeAlso}\relax
\code{\LinkA{Logic}{Logic}}, \code{\LinkA{which.min}{which.min}} for the index of
the minimum or maximum, and \code{\LinkA{match}{match}} for the first index of
an element in a vector, i.e., for a scalar \code{a}, \code{match(a,x)}
is equivalent to  \code{min(which(x == a))} but much more efficient.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
which(LETTERS == "R")
which(ll <- c(TRUE,FALSE,TRUE,NA,FALSE,FALSE,TRUE))#> 1 3 7
names(ll) <- letters[seq(ll)]
which(ll)
which((1:12)%%2 == 0) # which are even?
which(1:10 > 3, arr.ind=TRUE)

( m <- matrix(1:12,3,4) )
which(m %% 3 == 0)
which(m %% 3 == 0, arr.ind=TRUE)
rownames(m) <- paste("Case",1:3, sep="_")
which(m %% 5 == 0, arr.ind=TRUE)

dim(m) <- c(2,2,3); m
which(m %% 3 == 0, arr.ind=FALSE)
which(m %% 3 == 0, arr.ind=TRUE)

vm <- c(m)
dim(vm) <- length(vm) #-- funny thing with  length(dim(...)) == 1
which(vm %% 3 == 0, arr.ind=TRUE)
\end{ExampleCode}
\end{Examples}

