[Experimental]

vecvec_abind() generalises cbind()/rbind() to arbitrary-dimensional arrays, combining ... along the dimension given by along. It underlies the cbind()/rbind() methods for vecvec (along = 2 and along = 1 respectively), and can also be used directly to stack higher-dimensional vecvec arrays, e.g. combining two matrices into a 3D array with along = 3.

Inputs that aren't already vecvec objects are first wrapped with vecvec(). An input with no dim (a plain vector) is treated as having a single dimension of length 1 at along, so a bare vector becomes a single column (along = 2), row (along = 1), or slice (along > 2) - matching how cbind()/rbind() treat plain vectors. Every dimension other than along must match (after this padding) across all inputs.

vecvec_abind(..., along = 1L)

Arguments

...

Vectors or dim-annotated vecvec arrays to combine; wrapped with vecvec() automatically if not already vecvec objects. Named arguments (e.g. vecvec_abind(a = x, b = y)) name the corresponding position(s) along along, similar to cbind()/rbind().

along

The dimension along which to combine .... Defaults to 1 (row-wise, as rbind()). The result always has at least max(2, along) dimensions, so binding plain vectors produces a matrix (like cbind()/rbind()) rather than a flat vector - use c() for flat concatenation.

Value

A vecvec array whose dimension at along is the sum of the along dimensions of ... (after padding), and whose other dimensions match ....

See also

vecvec(); c() for flat (dim-less) concatenation of vecvec objects.

Examples

# cbind()-like: bind columns
vecvec_abind(vecvec(1:3), vecvec(4:6), along = 2)
#> <vecvec[3,2]>
#>      [,1] [,2]
#> [1,] 1    4   
#> [2,] 2    5   
#> [3,] 3    6   

# rbind()-like: bind rows (the default)
vecvec_abind(vecvec(1:3), vecvec(4:6))
#> <vecvec[2,3]>
#>      [,1] [,2] [,3]
#> [1,] 1    2    3   
#> [2,] 4    5    6   

# Stack two matrices into a 3D array
m1 <- array(vecvec(1:6), dim = c(2, 3))
m2 <- array(vecvec(7:12), dim = c(2, 3))
vecvec_abind(m1, m2, along = 3)
#> <vecvec[2,3,2]>
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,] 1    3    5   
#> [2,] 2    4    6   
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  7    9   11  
#> [2,]  8   10   12  
#>