[R] do.call("[", ...) question

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Sep 8 16:44:49 CEST 2004


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




More information about the R-help mailing list