ken - 6 months ago 23

R Question

I would like to obtain the indices of a 3D array in R (ie. arr[x,y,z]) based on multiple values. Specifically, using the first z dimension to subset values in the second z dimension. Here is a example:

`# create example array`

> m1 <- matrix(c(rep("a",5), rep("b",5), rep("c",5)), nr = 5)

> m2 <- matrix(c(rep(100, 5), rep(10, 5), rep(10, 5)), nr = 5)

> arr <- array(c(m1, m2), dim = c(dim(m1), 2))

#use which() to return the indices in m2 that correspond to indices with

#"a" and "c" values in m1. This does not work as expected.

> ac.ind <- which(arr[,,1] %in% c("a", "c"), arr.ind = T)

> ac.ind

[1] 1 2 3 4 5 11 12 13 14 15

which() returns an vector of positions in m1 that correspond to "a" and "c", not the matrix indices (the (x,y) positions). I would like ac.ind to return:

`row col`

[1,] 1 1

[2,] 2 1

[3,] 3 1

[4,] 4 1

[5,] 5 1

[1,] 1 3

[2,] 2 3

[3,] 3 3

[4,] 4 3

[5,] 5 3

If I do a more simple which() subset, it does return the indices:

`#use which to return indices in m2 that correspond to only "a" in m1`

>a.ind <- which(arr[,,1] == c("a"), arr.ind = T)

>a.ind

row col

[1,] 1 1

[2,] 2 1

[3,] 3 1

[4,] 4 1

[5,] 5 1

I am using %in% since I want to subset based on two values in m1 ("a" and "c" values). Is there a way to return the indices of an array based on two values in R?

Answer

The issue is that `arr[,,1] %in% c("a", "c")`

returns a vector. One way is to cast this as a `matrix`

with the number of rows equaling the first dimension of `arr`

:

```
ac.ind <- which(matrix(arr[,,1] %in% c("a", "c"), nrow=dim(arr)[1]), arr.ind = T)
## row col
## [1,] 1 1
## [2,] 2 1
## [3,] 3 1
## [4,] 4 1
## [5,] 5 1
## [6,] 1 3
## [7,] 2 3
## [8,] 3 3
## [9,] 4 3
##[10,] 5 3
```