# [R] arrayInd and which

Keith S Weintraub kw1958 at gmail.com
Wed Apr 3 16:53:56 CEST 2013

```Folks,

Any good examples or docs on what arrayInd does that is better or different from which()?

In addition take the following 20x10 matrix:

td<-structure(c(1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6,
6, 6, 1, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6,
6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6,
3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 2,
6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 5, 6, 6, 6, 6, 5, 6,
6, 3, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6, 6, 6, 6, 6, 6, 2, 6, 6,
4, 6, 6, 6, 6, 6, 6, 6, 6, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 6, 6, 6, 6, 6), .Dim = c(20L, 10L
))

I want to find the cells which (hah!) are <= c(rep(5,5), rep(4,5)). That is my bounds are by column.

Is there a better way to do this other than:

bounds<-c(rep(5,5), rep(4,5))
idxs<-which(apply(td, 2, "<=", bounds), arr.ind = TRUE)

> idxs
row col
[1,]   1   1
[2,]  13   1
[3,]  13   2
[4,]   1   3
[5,]   8   3
[6,]  13   3
[7,]   1   4
[8,]  13   4
[9,]   1   5
[10,]  13   5
[11,]   1   6
[12,]   4   6
[13,]  13   6
[14,]   4   7
[15,]  13   7
[16,]   1   8
[17,]   4   8
[18,]  13   8
[19,]   3   9
[20,]   1  10
[21,]  13  10

Lastly can you explain these results:

> td[idxs[10,]]
[1] 4 6

> td[idxs[10,1]]
[1] 4

> td[idxs[10,2]]
[1] 6

> td[idxs[10,3]]
Error: subscript out of bounds