<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/which-function-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://g4greetz.wordpress.com/2017/03/03/which-function-in-r/"> R – Greetz to Geeks</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <strong>which()</strong> function will return the position of the elements(i.e., row number/column number/array index) in a logical vector which are TRUE. Unlike the other base R functions, the which() will accept only the arguments with typeof as logical while the others will give an error.</p>
<p><strong>Syntax</strong>:<span> which(x, arr.ind = FALSE, useNames = TRUE)</span><br>
<span>                 arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE)</span></p>
<p><span>x</span>: can be a logical vector or an array. NAs are treated as FALSE and  allowed. But they will be omitted<br>
<span>arr.ind</span>: logical; should array indices be returned when x is an array?<br>
<span>ind</span>: integer-valued index vector, as resulting from which(x)<br>
<span>.dim</span>: integer vector<br>
<span>.dimnames</span>: optional list of character dimnames(.), of which only .dimnames[[1]] is used<br>
<span>useNames</span>: logical indicating if the value of arrayInd() should have (non-null) dimnames at all</p>
<p>Examples:</p>
<pre>
> which(letters == &quot;g&quot;)
[1] 7
</pre>
<pre>
> x &lt;- c(1,5,8,4,6)
> x
[1] 1 5 8 4 6
> which(x == 5)
[1] 2
> which(x != 5)
[1] 1 3 4 5
</pre>
<pre>
> datasets::npk
block N P K yield
1   1  0 1 1 49.5
2   1  1 1 0 62.8
3   1  0 0 0 46.8
4   1  1 0 1 57.0
5   2  1 0 0 59.8
6   2  1 1 1 58.5
7   2  0 0 1 55.5
8   2  0 1 0 56.0
9   3  0 1 0 62.8
10  3  1 1 1 55.8
11  3  1 0 0 69.5
12  3  0 0 1 55.0
13  4  1 0 0 62.0
14  4  1 1 1 48.8
15  4  0 0 1 45.5
16  4  0 1 0 44.2
17  5  1 1 0 52.0
18  5  0 0 0 51.5
19  5  1 0 1 49.8
20  5  0 1 1 48.8
21  6  1 0 1 57.2
22  6  1 1 0 59.0
23  6  0 1 1 53.2
24  6  0 0 0 56.0
> which(npk$yield == 62)
[1] 13
> which((npk$yield == 48.8) &amp; (npk$N == 1))
[1] 14
which(npk$yield == max(npk$yield))
[1] 11
</pre>
<pre>
> x &lt;- matrix(1:16,4,4)
> x
[,1] [,2] [,3] [,4]
[1,]  1    5   9    13
[2,]  2    6  10    14
[3,]  3    7  11    15
[4,]  4    8  12    16
> which.min(x)
[1] 1
> which.max(x)
[1] 16
> which(x %% 2 == 0)
[1] 2 4 6 8 10 12 14 16
> which(x %% 2 == 0, arr.ind=TRUE)
row col
[1,] 2 1
[2,] 4 1
[3,] 2 2
[4,] 4 2
[5,] 2 3
[6,] 4 3
[7,] 2 4
[8,] 4 4
> which(x %% 2 == 0, arr.ind=TRUE, useNames = FALSE)
[,1] [,2]
[1,] 2   1
[2,] 4   1
[3,] 2   2
[4,] 4   2
[5,] 2   3
[6,] 4   3
[7,] 2   4
[8,] 4   4
</pre>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/g4greetz.wordpress.com/1561/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/g4greetz.wordpress.com/1561/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://g4greetz.wordpress.com/2017/03/03/which-function-in-r/"> R – Greetz to Geeks</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/bbsLA-QwuT8"></a></div>