Skip to content
Prev 156850 / 398506 Next

Generalising to n-dimensions

Laura Bonnett wrote:
OK  I think I understand.  The magic package uses this type of 
construction extensively, but not this particular one.

It's trickier than I'd have expected.

Try this:

f <- function(a,v){
    jj <- 
sapply(dim(a)[seq_len(length(dim(a))-length(v))],seq_len,simplify=FALSE)
    jj <- c(jj , as.list(v))
    do.call("[" , c(list(a) , jj, drop=TRUE))
}



[you will have to coerce the output from expand.grid() to a matrix in 
order to extract a row from it]


HTH

rksh