which
Find TRUE Values

Description

Returns a vector of indices identifying the TRUE values of a logical vector.

Usage

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

Arguments

x A logical vector, a matrix, or an array.
arr.ind a logical value.
  • If FALSE (the default), and x is a matrix or an array, which returns vector indices of TRUE values of x.
  • If TRUE, and x is a matrix or an array, which returns a length(x) by length(dim(x)) matrix of the row, column, etc. indices of the TRUE values of x.
See the Value section for more details.
ind a numeric or complex value. NAs are allowed.
.dim a numeric vector, the length of which is the number column of returned value, and elements are used as modulo for ind.
.dimnames a list of length 2 giving a dimnames attribute for the matrix. Each component must either have length 0 or be a vector of character strings with length equal to the corresponding element of the dim attribute of the result.
useNames a logical value. if TRUE, set dimnames attribute for the returned matrix.

Details

if length of .dim is 2, use c("row", "col") as second element of "dimnames" attribute of returned matrix, else use dim1, dim2, ... as second element of "dimnames" attribute of returned matrix.
if length of .dim is no less than 2, modulo of ind is .dim[i] *.dim[i - 1], else is .dim[1].
Value
which If arr.ind=FALSE, returns the vector indices of the TRUE values in x, along with the corresponding names, if available, of x. If arr.ind=TRUE and x is an array, returns the row, column, etc. indices of the TRUE values in x. The values are returned in a matrix with one column for each dimension of the input array, along with the corresponding row names, if available, of x.
arrayInd Returns the same as which(array(x, dim=.dim, dimnames=.dimnames, arr.ind=TRUE, useNames=useNames).
See Also
[, Arithmetic, Logic
Examples
x <- c(10,20,30,NA,40) 
which(x>=30) 
x[which(x>=30)] 
which( cbind(c(T,F,F), c(F,F,T)), arr.ind=T)
tbl <- array(c(1,7,9,3), dim=c(2,2), dimnames=list(ID=c("A","B"), Attribute=c("Length","Width")))
which(tbl > 5, arr.ind=TRUE)
Package base version 6.0.0-69
Package Index