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! -- 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)
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! >
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
[Ooops. Forgot to copy the list 1st time around... Also missed the possibility of using t()] Robin Hankin <rksh at soc.soton.ac.uk> writes:> 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?Matrix indexing:> a[1,3,2,1][1] 11> b[rbind(c(1,3,2,1)[permutation])][1] 11 or, equivalently:> b[matrix(c(1,3,2,1)[permutation],1)][1] 11 or (neat, once you get it)> b[t(c(1,3,2,1)[permutation])][1] 11> 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!Less ghastly (but only slightly so):> do.call("[",c(list(b),as.list(c(1,3,2,1)[permutation])))[1] 11 -- 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