Skip to content

Adding zeros in each dimension of an array

4 messages · David Winsemius, Jeff Newmiller, Li Li

#
For a data frame, we can add an additional row or column easily. For
example, we can add an additional row of zero and an additional row of
column as follows.

Is there an easy and similar way to add zeros in each dimension? For
example, for
array(1:12, dim=c(2,2,3))?

Thanks for your help!!
   Hanna
1  1  5  9 13 17  0
2  2  6 10 14 18  0
3  3  7 11 15 19  0
4  4  8 12 16 20  0
5  0  0  0  0  0  0
#
Try installing the abind package.
And learn to post in plaint text.
David Winsemius
Alameda, CA, USA
#
Please read
https://www.lifewire.com/how-to-send-a-message-in-plain-text-from-gmail-1171963

Re your question: easy is in the eye of the beholder.

a <- array( 1:12, dim = c( 2, 2, 3 ) )
b <- array( 0, dim = dim( a ) + 1 )
b[ 1:2, 1:2, 1:3 ] <- a

If you want to do a lot of this, it could be wrapped for convenience 
though implementing that last expression without hardcoding the sequences 
may not be obvious:

expandArray <- function( a ) {
   b <- array( 0, dim = dim( a ) + 1 )
   do.call( `[<-`, c( list( b ), lapply( dim( a ), seq.int ), list( a ) ) )
}
expandArray( a )
On Wed, 7 Jun 2017, li li wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
#
Thanks! That is the method I am using now but I was checking whether there
is a simpler option. I will look into the abind package too.
   Hanna

2017-06-07 12:25 GMT-04:00 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>: