[R] Composing two n-dimensional arrays into one n+1-dimensional array

Bert Gunter gunter.berton at gene.com
Mon Jun 13 23:52:31 CEST 2011


Strangely enough, abind() in the abind package !!

-- Bert

("all edge cases" is too vague to guess whether abind meets your criteria)

On Mon, Jun 13, 2011 at 2:34 PM, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
> If I have 2 n-dimensional arrays, how do I compose them into a n+1-dimension
> array?
>
> Is there a standard R function that's something like the following, but that
> gives clean errors, handles all the edge cases, etc.
>
> abind <- function(a,b)  structure( c(a,b), dim = c(dim(a), 2) )
>
> m1 <- array(1:6,c(2,3))
> m2 <- m1 + 10
> abind(m1,m2)
>
> ==>
>
> , , 1
>
>     [,1] [,2] [,3]
> [1,]    1    3    5
> [2,]    2    4    6
>
> , , 2
>
>     [,1] [,2] [,3]
> [1,]   11   13   15
> [2,]   12   14   16
>
> Thanks,
>
>             -s
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list