[R] Index mapping on arrays

Demi Anderson demi.anderson at gmx.net
Thu Feb 1 14:34:21 CET 2007


Dear R-community,

I have some trouble with index mappings for arrays. If, for example,
I have the array

R> A <- array(1:9, c(3,3,2))

and two index mappings both of same size

R> x <- c(2, 3)
R> y <- c(1, 2)

Now I want to access the elements (A[1, x[i], y[i]])_i of A, i.e.
A[1, x[1], y[1]] = A[1, 2, 1] and A[1, x[2], y[2]] = A[1, 3, 2].

If I use

R> A[1, x, y]

I would get every combinations of indices of all elements of x and y
i.e. A[1, x[1], y[1]], A[1, x[1], y[2]], A[1, x[2], y[1]] and A[1,
x[2], y[2]]. But how can I access the elements (A[1, x[i], y[i]])_i.
My arrays dimensions are actually large in the second
component (for example the dimension might be 10*10000*10) so I'm
looking for a method avoiding loops.

The question is probably trivial for you, but I just could not
figure it out. So sorry for bugging you and many thanks in advance
for any help.

Best wishes, Demi Anderson.
-- 
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...



More information about the R-help mailing list