[R] aperm() and as.list() args to "["

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 10 13:50:55 CET 2004


On Wed, 10 Mar 2004, Robin Hankin wrote:

> 
> Hi everyone.
> 
> I'm playing with aperm():
> 
> a <- 1:24
> dim(a) <- c(2,3,2,2)
> permutation <-  c(1,2,4,3)
> b <- aperm(a,permutation)
> 
> 
> So if my understanding is right,
> 
> a[1,3,2,1] ==  b[c(1,3,2,1)[permutation] ]
> 
> but this isn't what I want because the RHS evaluates to a vector, and
> I am trying to identify a single element of b.
> 
> How do I modify the RHS to give what I want?

ind <- as.list(c(1,3,2,1)[permutation])
do.call("[", c(list(b), ind))


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list