EVOLUTION-MANAGER
Edit File: which.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Which indices are TRUE?</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for which {base}"><tr><td>which {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Which indices are TRUE?</h2> <h3>Description</h3> <p>Give the <code>TRUE</code> indices of a logical object, allowing for array indices. </p> <h3>Usage</h3> <pre> which(x, arr.ind = FALSE, useNames = TRUE) arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a <code><a href="logical.html">logical</a></code> vector or array. <code><a href="NA.html">NA</a></code>s are allowed and omitted (treated as if <code>FALSE</code>).</p> </td></tr> <tr valign="top"><td><code>arr.ind</code></td> <td> <p>logical; should <b>arr</b>ay <b>ind</b>ices be returned when <code>x</code> is an array?</p> </td></tr> <tr valign="top"><td><code>ind</code></td> <td> <p>integer-valued index vector, as resulting from <code>which(x)</code>.</p> </td></tr> <tr valign="top"><td><code>.dim</code></td> <td> <p><code><a href="dim.html">dim</a>(.)</code> integer vector</p> </td></tr> <tr valign="top"><td><code>.dimnames</code></td> <td> <p>optional list of character <code><a href="dimnames.html">dimnames</a>(.)</code>. If <code>useNames</code> is true, to be used for constructing dimnames for <code>arrayInd()</code> (and hence, <code>which(*, arr.ind=TRUE)</code>). If <code><a href="names.html">names</a>(.dimnames)</code> is not empty, these are used as column names. <code>.dimnames[[1]]</code> is used as row names.</p> </td></tr> <tr valign="top"><td><code>useNames</code></td> <td> <p>logical indicating if the value of <code>arrayInd()</code> should have (non-null) dimnames at all.</p> </td></tr> </table> <h3>Value</h3> <p>If <code>arr.ind == FALSE</code> (the default), an integer vector, or a double vector if <code>x</code> is a <em>long vector</em>, with <code>length</code> equal to <code>sum(x)</code>, i.e., to the number of <code>TRUE</code>s in <code>x</code>. </p> <p>Basically, the result is <code>(1:length(x))[x]</code> in typical cases; more generally, including when <code>x</code> has <code><a href="NA.html">NA</a></code>'s, <code>which(x)</code> is <code>seq_along(x)[!is.na(x) & x]</code> plus <code><a href="names.html">names</a></code> when <code>x</code> has. </p> <p>If <code>arr.ind == TRUE</code> and <code>x</code> is an <code><a href="array.html">array</a></code> (has a <code><a href="dim.html">dim</a></code> attribute), the result is <code>arrayInd(which(x), dim(x), dimnames(x))</code>, namely a matrix whose rows each are the indices of one element of <code>x</code>; see Examples below. </p> <h3>Note</h3> <p>Unlike most other base <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> functions this does not coerce <code>x</code> to logical: only arguments with <code><a href="typeof.html">typeof</a></code> logical are accepted and others give an error. </p> <h3>Author(s)</h3> <p>Werner Stahel and Peter Holzer (ETH Zurich) proposed the <code>arr.ind</code> option.</p> <h3>See Also</h3> <p><code><a href="Logic.html">Logic</a></code>, <code><a href="which.min.html">which.min</a></code> for the index of the minimum or maximum, and <code><a href="match.html">match</a></code> for the first index of an element in a vector, i.e., for a scalar <code>a</code>, <code>match(a, x)</code> is equivalent to <code>min(which(x == a))</code> but much more efficient.</p> <h3>Examples</h3> <pre> 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) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>