Skip to content
Prev 369581 / 398503 Next

Reversing one dimension of an array, in a generalized case

At the very real risk of flogging a dead horse, I realized that it is
slightly cleaner to use the logical index TRUE in the argument list of
`[` rather than the seq_len(dim[i])  to indicate "everything" in the
ith array dimension. I post on list because maybe this might be a bit
informative to others (as it took me a while to realize it). Here is
the modified version of my function incorporating this variation:

f2 <-function(a, i){
   d <- dim(a)
   l <- as.list(rep(TRUE, length(d))) ## instead of lapply() loop
   l[[i]] <- seq.int(d[i],1)  ## used rev() in prior version
   do.call("[", c(list(a), l))
}


It may also be a teeny tiny bit more efficient to do this way, albeit
unimportantly so.

As this reproduces my prior function, if that was not what you wanted,
neither is this.


Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jun 1, 2017 at 1:59 PM, Roy Mendelssohn - NOAA Federal
<roy.mendelssohn at noaa.gov> wrote: