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

Spencer Graves spencer.graves at pdf.com
Wed Mar 10 13:48:44 CET 2004


    How about: 

 > a <- 1:24
 > dim(a) <- c(2,3,2,2)
 > permutation <-  c(1,2,4,3)
 > b <- aperm(a,permutation)
 > a[1,3,2,1]
[1] 11
 >
 > Expr <- paste("b[", paste(c(1,3,2,1)[permutation], collapse=","), "]")
 > eval(parse(text=Expr))
[1] 11

      Ugly, I think, but effective. 
      hope this helps.  spencer graves

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?
>
>
> Following aren't right  either:
> b[as.vector(c(1,3,2,1)[permutation]) ]
> b[as.list(c(1,3,2,1)[permutation]) ]
>
>
> OBattempt:
> eval(parse(text=paste("b[",paste(c(1,3,2,1)[permutation],collapse=","),"]"))) 
>
>
> which DOES work, but is ghastly!
>




More information about the R-help mailing list