These functions bind multiple vectors, matrices, arrays, or rrays together
into one, combining along the .axis
.
rray_bind(..., .axis) rray_rbind(...) rray_cbind(...)
... | Vectors, matrices, arrays, or rrays. |
---|---|
.axis | A single integer. The axis to bind along. |
An array, or rray, depending on the input.
rray_bind()
is extremely flexible. It uses broadcasting to combine
arrays together in a way that the native functions of cbind()
and rbind()
cannot. See the examples section for more explanation!
# --------------------------------------------------------------------------- a <- matrix(1:4, ncol = 2) b <- matrix(5:6, ncol = 1) # Bind along columns rray_bind(a, b, .axis = 2)#> [,1] [,2] [,3] #> [1,] 1 3 5 #> [2,] 2 4 6# Bind along rows # Broadcasting is done automatically rray_bind(a, b, .axis = 1)#> [,1] [,2] #> [1,] 1 3 #> [2,] 2 4 #> [3,] 5 5 #> [4,] 6 6#> Error in rbind(a, b) : #> number of columns of matrices must match (see arg 2)# You can bind "up" to a new dimension # to stack matrices into an array rray_bind(a, b, .axis = 3)#> , , 1 #> #> [,1] [,2] #> [1,] 1 3 #> [2,] 2 4 #> #> , , 2 #> #> [,1] [,2] #> [1,] 5 5 #> [2,] 6 6 #># --------------------------------------------------------------------------- # Dimension name example x <- matrix( 1:6, ncol = 3, dimnames = list(c("a_r1", "a_r2"), c("a_c1", "a_c2", "a_c3")) ) y <- matrix( 7:8, ncol = 1, dimnames = list(NULL, c("b_c1")) ) # Dimension names come along for the ride # following rray name handling rray_bind(x, y, .axis = 2)#> a_c1 a_c2 a_c3 b_c1 #> a_r1 1 3 5 7 #> a_r2 2 4 6 8# If some inputs are named, and others # are not, the non-named inputs get `""` # as names rray_bind(x, y, .axis = 1)#> a_c1 a_c2 a_c3 #> a_r1 1 3 5 #> a_r2 2 4 6 #> 7 7 7 #> 8 8 8# You can add "outer" names to the # axis you are binding along. # They are added to existing names with `..` rray_bind(outer = x, y, .axis = 2)#> outer..a_c1 outer..a_c2 outer..a_c3 b_c1 #> a_r1 1 3 5 7 #> a_r2 2 4 6 8# Outer names can be used to give unnamed # inputs default names rray_bind(outer = x, outer_y = y, .axis = 1)#> a_c1 a_c2 a_c3 #> outer..a_r1 1 3 5 #> outer..a_r2 2 4 6 #> outer_y1 7 7 7 #> outer_y2 8 8 8