From ligges at statistik.tu-dortmund.de  Fri Dec  5 18:13:46 2008
From: ligges at statistik.tu-dortmund.de (Uwe Ligges)
Date: Fri, 05 Dec 2008 18:13:46 +0100
Subject: [Rd] array indexing
In-Reply-To: <493944D1.7060903@cam.ac.uk>
References: <493944D1.7060903@cam.ac.uk>
Message-ID: <4939614A.3080107@statistik.tu-dortmund.de>



Robin Hankin wrote:
> Hi.
> 
> I have been pondering array indexing via matrices.
> 
>  > a <- array(1:27,rep(3,3))
>  >  index <- matrix(c(1,1,1,1,2,3),2,3,byrow=TRUE)
>  > a[index]
> [1]  1 22
> 
> 
> as expected and documented.  But what was the thinking
> behind the decision to access the array by rows rather
> than columns?
> 
> The 'index' matrix is ordered as [1,1,1,2,1,3] and so
> the extraction is  a[index[c(1,3,5)]] for the first element
> and a[index[c(2,3,6)]] for the second.
> If the indexing was by columns then we would have
> 
> <fantasy>
>  > a <- array(1:27,rep(3,3))
>  >  index <- matrix(c(1,1,1,1,2,3),2,3)
>  > a[index]
> [1]  1 22
> </fantasy>
> 
> Thus the extraction is a[index[1:3]] and a[index[4:6]].
> 
> This seems to be a sensible way of going about array indexing.
> For example, even *defining* the 'index'
> matrix is simpler.
> 
> So, what is the thinking behind the  behaviour as implemented?
> 
> I'm asking because I want to understand the thinking behind the
> decision.


 From my point of view common thinking with matrices and data.frames is 
that one row corresponds to one observation (that should be extracted).

Uwe Ligges

> 
> rksh
>



[Rd] array indexing
Uwe Ligges