Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
2009-Apr-15 18:33 UTC
[R] (hopefully) simple array op
I have a multidimensional array "a", for example,> a, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 So a is 2x2x2. I have another array "b", for example,> b[,1] [,2] [1,] 9 11 [2,] 10 12 So b is 2x2. I want to "tack" b onto a so that b becomes a new "plane", so to speak, in a. In other words, I want the result c to be:> c, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 I can think of a number of ways to do this, but they are all cumbersome. Given R's facility with arrays and indices, it occurs to me that there might be some "one-line" way to accomplish this. Does anyone know if there is? Thanks. --Sam
Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
2009-Apr-15 18:42 UTC
[R] (hopefully) simple array op
Regarding my last mail, I typed too soon. Figure I can do> a<-array(c(a,b),dim=c(2,2,3))Seems to work fine. But I would like to hear other ideas if you have any. In particular, I am going to have to do this operation over and over. That last index "3" will have to increase by one every time. Seems like it would make for nicer code if I didn't have to worry about that. Thanks again.
Do you want abind? http://cran.r-project.org/web/packages/abind/index.html baptiste On 15 Apr 2009, at 19:33, Cable, Samuel B Civ USAF AFMC AFRL/RVBXI wrote:> I have a multidimensional array "a", for example, > >> a > , , 1 > > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > , , 2 > > [,1] [,2] > [1,] 5 7 > [2,] 6 8 > > So a is 2x2x2. > > I have another array "b", for example, > >> b > [,1] [,2] > [1,] 9 11 > [2,] 10 12 > > So b is 2x2. > > I want to "tack" b onto a so that b becomes a new "plane", so to > speak, > in a. In other words, I want the result c to be: > >> c > , , 1 > > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > , , 2 > > [,1] [,2] > [1,] 5 7 > [2,] 6 8 > > , , 3 > > [,1] [,2] > [1,] 9 11 > [2,] 10 12 > > I can think of a number of ways to do this, but they are all > cumbersome. > Given R's facility with arrays and indices, it occurs to me that there > might be some "one-line" way to accomplish this. Does anyone know if > there is? Thanks. > > --Sam > > ______________________________________________ > 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag