複数の値に基づいてRの3D配列のインデックス(つまりarr [x、y、z])を取得したいと考えています。具体的には、第1のz次元を使用して第2のz次元の値をサブセット化する。 (は)に "a" および "C" ではなく、行列インデックス((x、y)の位置)に対応するM1の位置のベクトルを返す複数の値に基づいてRの3D配列のインデックスを返します
# 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
:ここでは一例です。私はac.indが返すしたいと思います:
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
私はよりシンプル()サブセットをすれば、それはインデックスを返す行います
#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
私がしたいので、私は%で%を使用していますm1( "a"と "c"の値)の2つの値に基づくサブセット。 Rの2つの値に基づいて配列のインデックスを返す方法はありますか?
また、 '?arrayInd'も参照してください。 'arrayInd(ac.ind、dim(arr)[1:2])' –