[R] Index mapping on arrays

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Feb 1 14:48:25 CET 2007


probably you want something like the following:

A[cbind(rep(1, length(x)), x, y)]


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Demi Anderson" <demi.anderson at gmx.net>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, February 01, 2007 2:34 PM
Subject: [R] Index mapping on arrays


> 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 ...
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list