Adding arrays (rbind for> 2 dimensions)

I have a series of calls that leave me with a list of arrays. The first size is different in number and the two measurements are the same. I want to place each row (first dimension) on top of each other, just like it rbind

would do for a 2d array (matrix).

The data looks like this:

la <- list( array( dim=c(1,6,7) ), array( dim=c(2,6,7) ), array( dim=c(3,6,7) ) )

      

I cannot use do.call(rbind, la)

because it just aligns it to matrix.

I could write something like:

rbind.array <- function(..., deparse.level=1) {
   allargs <- list(...)
   n <- length(allargs)
   dims <- sapply(la,dim)
   sel.dim <- which(!apply( dims, 1, function(x) length(unique(x))==1 ))
   stopifnot(length(sel.dim)==1)
   target.length <- apply( dims, 1, sum)[sel.dim]
   target.dims <- dims[,1]
   target.dims[sel.dim] <- target.length
   res <- array( dim=target.dims )
   for( i in seq(n) ) {
      # stack array slices one by one
   }
} 

      

But surely such a function already exists?

+3


source to share


1 answer


I think abind

from abind doing this ...?

library(abind)
abind(la[[1]],la[[2]],la[[3]],along = 1)

      



In addition to the solution, Curry

this also works:

do.call("abind",list(la,along = 1))

      

+3


source







All Articles