I was hoping to be able to construct a do.call
formula for subsetting without having to identify the actual range of every dimension in the input array.
The problem I'm running into is that I can't figure out how to mimic the direct function x[,,1:n,]
, where no entry in the other dimensions means "grab all elements."
Here's some sample code, which fails. So far as I can tell, either [
or do.call
replaces my NULL
list values with 1
for the index.
x<-array(1:6,c(2,3))
dimlist<-vector('list', length(dim(x)))
shortdim<-2
dimlist[[shortdim]] <- 1: (dim(x)[shortdim] -1)
flipped <- do.call(`[`,c(list(x),dimlist))
I suppose I could kludge a solution by assigning the value -2*max(dim(x))
to each element of dimlist
, but yuck.
(FWIW, I have alternate functions which do the desired job either via melt/recast
or the dreaded "build a string and then eval(parse(mystring))
, but I wanted to do it "better.")
Edit: as an aside, I ran a version of this code (with the equivalent of DWin's TRUE setup) against a function which used melt & acast
; the latter was several times slower to no real surprise.
flipdim
. AnddimList
was an empty list after its creation because there didn't happen to be anx
object in my workspace. There is now, and it's not clear whether you really wanted dimlist to be 6 items-long. – Ivied(dimlist<-vector('list', length(dim(x)))
throws an error. – Iviedalist
instead of alist
. – Leavitt