[R] aperm() and as.list() args to "["
Robin Hankin
rksh at soc.soton.ac.uk
Wed Mar 10 17:21:08 CET 2004
Many thanks to everyone who replied (online and offline) to my query
about permutations.
There appear to be two independent ways of solving it:
(1) matrix indexing, as in
b[rbind(c(1,3,2,1)[permutation])]
(2) do.call(), as in
ind <- as.list(c(1,3,2,1)[permutation])
do.call("[", c(list(b), ind))
I found both these very instructive (us ex-matlab users often
overlook matrix indexing!) but to me the
do.call() solution is positively sublime (Peter, why didn't you like
it? let's get an aesthetics discussion going!)
I spent some considerable time over lunch today reading the helppages
for do.call(), "[", and c().
There is no way under the Sun that I would have come up with the
second command above.
Could we add this example or one like it to help(do.call)?
Also, the help page for "[" doesn't include what do.call() did, namely
"["(b,1,3,1,2)
could we add this or some similar example to ?"[" please?
cheers
rksh
>
> >
>> a <- 1:24
>> dim(a) <- c(2,3,2,2)
>> permutation <- c(1,2,4,3)
>> b <- aperm(a,permutation)
> >
> >
--
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)
More information about the R-help
mailing list