简体   繁体   English

如何解释 which() output 中的数组索引?

[英]How to interpret array indices in which() output?

I'm very new to R, so this is very much a beginner's question.我是 R 的新手,所以这是一个初学者的问题。

I'm trying to read/interpret the returned matrix from a which(x, arr.ind = TRUE) command.我正在尝试读取/解释从which(x, arr.ind = TRUE)命令返回的矩阵。

The data I'm using is:我使用的数据是:

qux <- array(data = c(10,5,1,4,7,4,3,3,1,3,4,3,1,7,8,3,7,3), dim = c(3, 2, 3))

I run which() while filtering for values lower than 3 and tell the which() function to return array indices, using arr.ind = TRUE .我在过滤小于 3 的值时运行which()并使用arr.ind = TRUE告诉which() function 返回数组索引。

> which(qux<3, arr.ind = TRUE)

     dim1 dim2 dim3
[1,]    3    1    1
[2,]    3    1    2
[3,]    1    1    3

I would assume the numbers in the left most column indicate the rows (of which there are 3 in the array), the other three columns to the right indicate the dimensions (of which there are also 3), and the numbers that make up the matrix are the column numbers.我假设最左边一列中的数字表示行(数组中有 3 行),右边的其他三列表示维度(其中也有 3 行),以及构成矩阵是列号。

However, this can't be true since there are only two columns in all the dimensions of the array, whereas there's three 3's in the matrix.然而,这不可能是真的,因为在数组的所有维度中只有两列,而矩阵中有三个 3。

Please help me understand this elementary part of R.请帮助我理解 R 的这个基本部分。

Kind regards.亲切的问候。

Your qux is a three dimensional array with exactly three values that are 1, the rest are greater than three.您的qux是一个三维数组,恰好有三个值为 1,rest 大于三个。

The output you see gives the positions for those values that are less than three您看到的 output 给出了那些小于三的值的位置

which(qux<3, arr.ind = TRUE)
#      dim1 dim2 dim3
# [1,]    3    1    1
# [2,]    3    1    2
# [3,]    1    1    3

and

qux[3,1,1]
# [1] 1
qux[3,1,2]
# [1] 1
qux[1,1,3]
# [1] 1

so each row of the which() matrix gives you the index for each of the three dimensions in your original array.因此which()矩阵的每一行都为您提供原始数组中三个维度中每个维度的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM