Hi again everyone I have an arbitrarily dimensional array "a" and a list "jj" of length length(dim(a)). The elements of jj are vectors of indexes. How do I use do.call() to extract a[ jj[[1]], jj[[2]], jj[[3]], ...] ? Toy example follows: a <- matrix(1:30,5,6) jj <- list(5:1,6:1) I want the following a[ jj[[1]],jj[[2]] ] How do I do this? OBAttempts: do.call("[",list(a,jj)) do.call("[",c(a,jj)) do.call("[",list(a,unlist(jj))) Of course, the one that works is do.call("[",list(a,jj[[1]],jj[[2]])) but I don't know how long jj is apriori so this won't do. -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)
Hi Robin, you could try the following: lis <- lapply(seq(1, length(dim(a))+1), function(x,a.,jj.) if(x==1) a. else jj.[[x-1]] ,a.=a, jj.=jj) do.call("[",lis) I hope this 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/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm/ ----- Original Message ----- From: "Robin Hankin" <rksh at soc.soton.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, September 08, 2004 4:08 PM Subject: [R] do.call("[", ...) question> Hi again everyone > > I have an arbitrarily dimensional array "a" and a list "jj" oflength> length(dim(a)). The elements of jj are vectors of indexes. > > How do I use do.call() to extract a[ jj[[1]], jj[[2]], jj[[3]], ...]?> > > Toy example follows: > > a <- matrix(1:30,5,6) > jj <- list(5:1,6:1) > > I want the following > > a[ jj[[1]],jj[[2]] ] > > How do I do this? > > > > OBAttempts: > > do.call("[",list(a,jj)) > do.call("[",c(a,jj)) > do.call("[",list(a,unlist(jj))) > > > Of course, the one that works is > > do.call("[",list(a,jj[[1]],jj[[2]])) > > but I don't know how long jj is apriori so this won't do. > -- > Robin Hankin > Uncertainty Analyst > Southampton Oceanography Centre > SO14 3ZH > tel +44(0)23-8059-7743 > initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spamprecaution)> > ______________________________________________ > 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
Robin Hankin <rksh at soc.soton.ac.uk> writes:> Hi again everyone > > I have an arbitrarily dimensional array "a" and a list "jj" of length > length(dim(a)). The elements of jj are vectors of indexes. > > How do I use do.call() to extract a[ jj[[1]], jj[[2]], jj[[3]], ...] ? > > > Toy example follows: > > a <- matrix(1:30,5,6) > jj <- list(5:1,6:1) > > I want the following > > a[ jj[[1]],jj[[2]] ] > > How do I do this? > > > > OBAttempts: > > do.call("[",list(a,jj)) > do.call("[",c(a,jj)) > do.call("[",list(a,unlist(jj))) > > > Of course, the one that works is > > do.call("[",list(a,jj[[1]],jj[[2]])) > > but I don't know how long jj is apriori so this won't do.do.call("[",c(list(a),jj)) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
This worked very well for me: do.call("[", c(list(a), jj)) What about you? Kevin -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Robin Hankin Sent: Wednesday, September 08, 2004 7:08 AM To: r-help at stat.math.ethz.ch Subject: [R] do.call("[", ...) question Hi again everyone I have an arbitrarily dimensional array "a" and a list "jj" of length length(dim(a)). The elements of jj are vectors of indexes. How do I use do.call() to extract a[ jj[[1]], jj[[2]], jj[[3]], ...] ? Toy example follows: a <- matrix(1:30,5,6) jj <- list(5:1,6:1) I want the following a[ jj[[1]],jj[[2]] ] How do I do this? OBAttempts: do.call("[",list(a,jj)) do.call("[",c(a,jj)) do.call("[",list(a,unlist(jj))) Of course, the one that works is do.call("[",list(a,jj[[1]],jj[[2]])) but I don't know how long jj is apriori so this won't do. -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution) ______________________________________________ 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