R中了解数组索引 [英] Understanding array indexing in R

查看:119
本文介绍了R中了解数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阵列的 R文件状态

在数据向量的值给出了相同的顺序,因为他们将出现在FORTRAN数组中的值,即一栏大订单,与第一标移动速度最快,最后下标慢

The values in the data vector give the values in the array in the same order as they would occur in FORTRAN, that is "column major order," with the first subscript moving fastest and the last subscript slowest.

再后来给阐明的例子此通过将数据装载到二维阵列

It then later gives a clarifying example of this by loading data into a two dimensional array:

 > x <- array(1:20, dim=c(4,5))   # Generate a 4 by 5 array.
 > x
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    5    9   13   17
 [2,]    2    6   10   14   18
 [3,]    3    7   11   15   19
 [4,]    4    8   12   16   20

这与其他语言的经验,我想 X [1,2] ,而不是 X [2,1] ,将 2 ,但这是pretty容易调整自己的思维。然而,就以最快的速度我作心智模式的转变,接下来的例子捣毁它拆开:

From experience with other languages, I would think x[1, 2], rather than x[2, 1], would be 2, but this is pretty easy to adjust my thinking to. However, just as quickly as I perform my mental model shift, the next example smashes it apart:

 > i <- array(c(1:3,3:1), dim=c(3,2))
 > i                             # i is a 3 by 2 index array.
      [,1] [,2]
 [1,]    1    3
 [2,]    2    2
 [3,]    3    1
 > x[i]                          # Extract those elements
 [1] 9 6 3

所以,我可以看到这里发生的事情是,我们提取的元素 X [1,3] X [2,2] X [3,1] 。好吧,但不会这样做完全违背上述要求的一栏主要为了?

So, I can see what happened here is that we extracted elements x[1, 3], x[2,2], and x[3, 1]. Okay, but doesn't this run completely counter to above claim of "column major order"?

从我的理解, I 应该是一个2 3 的阵列,和R应该有间$ P $的 PTED X [I] X [I [1,1],I [2,1],X [I [1,2],我[ 2,2],... 。但是,我们观察到那是什么,而不是,R做了 X [I [1,1],I [1,2],X [I [2,1],I [2,2] ] ...

From what I had understood, i should have been a 2 by 3 array, and R should have interpreted x[i] as x[i[1, 1], i[2, 1]], x[i[1, 2], i[2, 2]], .... However, what we observe is that, instead, R did x[i[1, 1], i[1, 2]], x[i[2, 1], i[2, 2]], ...

这是R中的一个基本的不一致或者有我完全误解的文档?

Is this a fundamental inconsistency in R, or have I completely misunderstood the documentation?

推荐答案

列重大令仅仅意味着内部矩阵向量按列排序的列;看到

"column major order" only means that internally matrices are vectors ordered column by column; see that

x[1:20]

1,2,3,...,19,20 。外形尺寸是有序像往常一样在科学 - 第一行,则列,那么深,那么hyperdepth ...
第三个例子是棘手的 - 如果i具有相同的列数是x具有的尺寸,它是PTED作为矢量选择除$ P $。如果不是这样,既 I X 是美滋滋地逐列向量和简单的矢量指数化规则适用于...例如 X [T(I)] 1:20 [C(1,3,2,2,3,1)]

is 1, 2, 3, ..., 19, 20. Dimensions are ordered as usual in science -- first rows, then columns, then depth, then hyperdepth... The third example is tricky -- if i has the same number of columns that x has dimensions, it is interpreted as vectorized selection. If not, both i and x are flattered to by-column vectors and simple vector indexation rules apply... for instance x[t(i)] is 1:20[c(1,3,2,2,3,1)].

这篇关于R中了解数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆