[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  which: Which indices are TRUE?

##  Description

Give the ` TRUE ` indices of a logical object, allowing for array indices.

##  Usage

    
    
    which(x, arr.ind = FALSE, useNames = TRUE)
    arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE)

##  Arguments

x

a ` [ logical ](/link/logical?package=base&version=3.6.2) ` vector or array. `
[ NA ](/link/NA?package=base&version=3.6.2) ` s are allowed and omitted
(treated as if ` FALSE ` ).

arr.ind

logical; should **arr** ay **ind** ices be returned when ` x ` is an array?

ind

integer-valued index vector, as resulting from ` which(x) ` .

.dim

` [ dim ](/link/dim?package=base&version=3.6.2) (.) ` integer vector

.dimnames

optional list of character ` [ dimnames
](/link/dimnames?package=base&version=3.6.2) (.) ` . If ` useNames ` is true,
to be used for constructing dimnames for ` arrayInd() ` (and hence, ` which(*,
arr.ind=TRUE) ` ). If ` [ names ](/link/names?package=base&version=3.6.2)
(.dimnames) ` is not empty, these are used as column names. ` .dimnames[[1]] `
is used as row names.

useNames

logical indicating if the value of ` arrayInd() ` should have (non-null)
dimnames at all.

##  Value

If ` arr.ind == FALSE ` (the default), an integer vector, or a double vector
if ` x ` is a _long vector_ , with ` length ` equal to ` sum(x) ` , i.e., to
the number of ` TRUE ` s in ` x ` .

Basically, the result is ` (1:length(x))[x] ` in typical cases; more
generally, including when ` x ` has ` [ NA
](/link/NA?package=base&version=3.6.2) ` 's, ` which(x) ` is `
seq_along(x)[!is.na(x) & x] ` plus ` [ names
](/link/names?package=base&version=3.6.2) ` when ` x ` has.

If ` arr.ind == TRUE ` and ` x ` is an ` [ array
](/link/array?package=base&version=3.6.2) ` (has a ` [ dim
](/link/dim?package=base&version=3.6.2) ` attribute), the result is `
arrayInd(which(x), dim(x), dimnames(x)) ` , namely a matrix whose rows each
are the indices of one element of ` x ` ; see Examples below.

##  See Also

` [ Logic ](/link/Logic?package=base&version=3.6.2) ` , ` [ which.min
](/link/which.min?package=base&version=3.6.2) ` for the index of the minimum
or maximum, and ` [ match ](/link/match?package=base&version=3.6.2) ` for the
first index of an element in a vector, i.e., for a scalar ` a ` , ` match(a,
x) ` is equivalent to ` min(which(x == a)) ` but much more efficient.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fwhich&utm_source=r-docs&utm_medium=docs&utm_term=which&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    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) )
    div.3 <- m %% 3 == 0
    which(div.3)
    which(div.3, 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(div.3, arr.ind = FALSE)
    which(div.3, arr.ind = TRUE)
    
    vm <- c(m)
    dim(vm) <- length(vm) #-- funny thing with  length(dim(...)) == 1
    which(div.3, arr.ind = TRUE)
    # }
    # NOT RUN {
    <!-- %dont -->
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fwhich&utm_source=r-docs&utm_medium=docs&utm_term=which&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)