R Under development (unstable) (2024-09-10 r87115 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(abind) > # unlike cbind or rbind > abind(x=1:4,y=5:8) x1 x2 x3 x4 y1 y2 y3 y4 1 2 3 4 5 6 7 8 > # like cbind > abind(x=1:4,y=5:8,along=2) x y [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > abind(x=1:4,matrix(5:20,nrow=4),along=2) x [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > abind(1:4,matrix(5:20,nrow=4),along=2) [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > # like rbind > abind(x=1:4,matrix(5:20,nrow=4),along=1) [,1] [,2] [,3] [,4] x 1 2 3 4 5 9 13 17 6 10 14 18 7 11 15 19 8 12 16 20 > abind(1:4,matrix(5:20,nrow=4),along=1,make.names=TRUE) [,1] [,2] [,3] [,4] 1:4 1 2 3 4 matrix(5:20, nrow = 4)1 5 9 13 17 matrix(5:20, nrow = 4)2 6 10 14 18 matrix(5:20, nrow = 4)3 7 11 15 19 matrix(5:20, nrow = 4)4 8 12 16 20 > # different default dimnames: > abind(x=1:4,matrix(5:20,nrow=4),along=1) [,1] [,2] [,3] [,4] x 1 2 3 4 5 9 13 17 6 10 14 18 7 11 15 19 8 12 16 20 > abind(x=1:4,matrix(5:20,nrow=4),along=1,force.array=FALSE) [,1] [,2] [,3] [,4] x 1 2 3 4 5 9 13 17 6 10 14 18 7 11 15 19 8 12 16 20 > # concatenates two vectors: > abind(x=1:4,y=5:8) x1 x2 x3 x4 y1 y2 y3 y4 1 2 3 4 5 6 7 8 > abind(x=c(a=1,b=2),y=3:4) a b y1 y2 1 2 3 4 > abind(x=c(a=1,b=2),y=c(c=3,d=4)) a b c d 1 2 3 4 > # simulate rbind with row vectors in three ways: > # (1) easiest way: insert new dimension before 1 (use any number less than 1 for along) > abind(x=1:4,y=5:8,along=0.5) [,1] [,2] [,3] [,4] x 1 2 3 4 y 5 6 7 8 > abind(x=c(a=1,b=2),y=c(c=3,d=4), along=0) # with names c d x 1 2 y 3 4 > abind(x=c(a=1,b=2),y=c(c=3,d=4), along=0, use.first.dimnames=TRUE) a b x 1 2 y 3 4 > # (2) permute the result: > aperm(abind(1:4,5:8,along=2),c(2,1)) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 > # different default dimnames: > aperm(abind(1:4,5:8,along=2,make.names=TRUE),c(2,1)) [,1] [,2] [,3] [,4] 1:4 1 2 3 4 5:8 5 6 7 8 > # (3) convert arguments to row vectors > abind(matrix(1:4,nrow=1),matrix(5:8,nrow=1),along=1) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 > # bind two matrices, 5 possible values for along > abind(x=matrix(1:16,nrow=4),y=matrix(17:32,nrow=4),along=1) [,1] [,2] [,3] [,4] x1 1 5 9 13 x2 2 6 10 14 x3 3 7 11 15 x4 4 8 12 16 y1 17 21 25 29 y2 18 22 26 30 y3 19 23 27 31 y4 20 24 28 32 > abind(x=matrix(1:16,nrow=4),y=matrix(17:32,nrow=4),along=2) x1 x2 x3 x4 y1 y2 y3 y4 [1,] 1 5 9 13 17 21 25 29 [2,] 2 6 10 14 18 22 26 30 [3,] 3 7 11 15 19 23 27 31 [4,] 4 8 12 16 20 24 28 32 > abind(x=matrix(1:16,nrow=4),y=matrix(17:32,nrow=4),along=3) , , x [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 , , y [,1] [,2] [,3] [,4] [1,] 17 21 25 29 [2,] 18 22 26 30 [3,] 19 23 27 31 [4,] 20 24 28 32 > abind(x=matrix(1:16,nrow=4),y=matrix(17:32,nrow=4),along=0.5) , , 1 [,1] [,2] [,3] [,4] x 1 2 3 4 y 17 18 19 20 , , 2 [,1] [,2] [,3] [,4] x 5 6 7 8 y 21 22 23 24 , , 3 [,1] [,2] [,3] [,4] x 9 10 11 12 y 25 26 27 28 , , 4 [,1] [,2] [,3] [,4] x 13 14 15 16 y 29 30 31 32 > abind(x=matrix(1:16,nrow=4),y=matrix(17:32,nrow=4),along=1.5) , , 1 x y [1,] 1 17 [2,] 2 18 [3,] 3 19 [4,] 4 20 , , 2 x y [1,] 5 21 [2,] 6 22 [3,] 7 23 [4,] 8 24 , , 3 x y [1,] 9 25 [2,] 10 26 [3,] 11 27 [4,] 12 28 , , 4 x y [1,] 13 29 [2,] 14 30 [3,] 15 31 [4,] 16 32 > # examples with three matrices > cc <- as.data.frame(matrix(25:36,nrow=3)) > aa <- matrix(1:12,nrow=3,dimnames=list(letters[1:3],LETTERS[1:4])) > # Note that names on cc are lost with as.matrix > rownames(cc) [1] "1" "2" "3" > rownames(as.matrix(cc)) NULL > abind(a=aa, cc, matrix(25:36,3,4), along=0, use.first.dimnames=TRUE) , , A a b c a 1 2 3 25 26 27 25 26 27 , , B a b c a 4 5 6 28 29 30 28 29 30 , , C a b c a 7 8 9 31 32 33 31 32 33 , , D a b c a 10 11 12 34 35 36 34 35 36 > abind(a=aa, cc, matrix(25:36,3,4), along=1, use.first.dimnames=TRUE) A B C D a 1 4 7 10 b 2 5 8 11 c 3 6 9 12 25 28 31 34 26 29 32 35 27 30 33 36 25 28 31 34 26 29 32 35 27 30 33 36 > abind(a=aa, cc, matrix(25:36,3,4), along=1.1, use.first.dimnames=TRUE) , , A a a 1 25 25 b 2 26 26 c 3 27 27 , , B a a 4 28 28 b 5 29 29 c 6 30 30 , , C a a 7 31 31 b 8 32 32 c 9 33 33 , , D a a 10 34 34 b 11 35 35 c 12 36 36 > abind(a=aa, cc, matrix(25:36,3,4), along=2) A B C D V1 V2 V3 V4 a 1 4 7 10 25 28 31 34 25 28 31 34 b 2 5 8 11 26 29 32 35 26 29 32 35 c 3 6 9 12 27 30 33 36 27 30 33 36 > abind(a=aa, cc, matrix(25:36,3,4), along=2, use.first.dimnames=TRUE) A B C D V1 V2 V3 V4 a 1 4 7 10 25 28 31 34 25 28 31 34 b 2 5 8 11 26 29 32 35 26 29 32 35 c 3 6 9 12 27 30 33 36 27 30 33 36 > abind(a=aa, cc, matrix(25:36,3,4), along=3, use.first.dimnames=TRUE) , , a A B C D a 1 4 7 10 b 2 5 8 11 c 3 6 9 12 , , A B C D a 25 28 31 34 b 26 29 32 35 c 27 30 33 36 , , A B C D a 25 28 31 34 b 26 29 32 35 c 27 30 33 36 > abind(a=aa, cc, matrix(25:36,3,4), along=3, make.names=TRUE, use.first.dimnames=TRUE) , , a A B C D a 1 4 7 10 b 2 5 8 11 c 3 6 9 12 , , cc A B C D a 25 28 31 34 b 26 29 32 35 c 27 30 33 36 , , matrix(25:36, 3, 4) A B C D a 25 28 31 34 b 26 29 32 35 c 27 30 33 36 > abind(a=aa, cc, dd=matrix(25:36,3,4), along=1.1, use.first.dimnames=TRUE) , , A a dd a 1 25 25 b 2 26 26 c 3 27 27 , , B a dd a 4 28 28 b 5 29 29 c 6 30 30 , , C a dd a 7 31 31 b 8 32 32 c 9 33 33 , , D a dd a 10 34 34 b 11 35 35 c 12 36 36 > x1 <- array(1:8,dim=c(2,2,2),dimnames=list(letters[6:7],letters[1:2],letters[24:25])) > x1 , , x a b f 1 3 g 2 4 , , y a b f 5 7 g 6 8 > # test that we get dimnames correctly when we need to expand dimensions > x2.1 <- array(11:14,dim=c(2,2),dimnames=list(letters[1:2],letters[24:25])) > x2.2 <- array(11:14,dim=c(2,2),dimnames=list(letters[6:7],letters[24:25])) > x2.3 <- array(11:14,dim=c(2,2),dimnames=list(letters[6:7],letters[1:2])) > abind(x1, h=x2.1, along=1) , , x a b f 1 3 g 2 4 h 11 12 , , y a b f 5 7 g 6 8 h 13 14 > abind(x1, c=x2.2, along=2) , , x a b c f 1 3 11 g 2 4 12 , , y a b c f 5 7 13 g 6 8 14 > abind(x1, z=x2.3, along=3) , , x a b f 1 3 g 2 4 , , y a b f 5 7 g 6 8 , , z a b f 11 13 g 12 14 > # Five different ways of binding together two matrices > x <- matrix(1:12,3,4) > y <- x+100 > dim(abind(x,y,along=0)) [1] 2 3 4 > dim(abind(x,y,along=1)) [1] 6 4 > dim(abind(x,y,along=1.5)) [1] 3 2 4 > dim(abind(x,y,along=2)) [1] 3 8 > dim(abind(x,y,along=3)) [1] 3 4 2 > dim(abind(x,y,rev.along=0)) [1] 3 4 2 > dim(abind(x,y,rev.along=1)) [1] 3 8 > # using a list argument > abind(list(x=1:4,y=5:8)) x1 x2 x3 x4 y1 y2 y3 y4 1 2 3 4 5 6 7 8 > abind(list(x=1:4,y=5:8),along=2) x y [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > abind(list(x=1:4,matrix(5:20,nrow=4)),along=2) x [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > abind(list(1:4,matrix(5:20,nrow=4)),along=2) [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > L <- list(1:4,matrix(5:20,nrow=4)) > abind(L,along=2) [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > abind(L,along=1) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 9 13 17 [3,] 6 10 14 18 [4,] 7 11 15 19 [5,] 8 12 16 20 > L <- list(x=1:4,matrix(5:20,nrow=4)) > abind(L,along=2) x [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > # Equivalent call to cbind > do.call("cbind", L) x [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > # Equivalent call to rbind > abind(L,along=1) [,1] [,2] [,3] [,4] x 1 2 3 4 5 9 13 17 6 10 14 18 7 11 15 19 8 12 16 20 > do.call("rbind", L) [,1] [,2] [,3] [,4] x 1 2 3 4 5 9 13 17 6 10 14 18 7 11 15 19 8 12 16 20 > L <- list(x=1:4,y=5:8) > abind(L,along=0) [,1] [,2] [,3] [,4] x 1 2 3 4 y 5 6 7 8 > abind(L,along=1) x1 x2 x3 x4 y1 y2 y3 y4 1 2 3 4 5 6 7 8 > abind(L,along=2) x y [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > # behavior with NULL objects > dim(abind(list(a=NULL, b=NULL), along=1)) NULL > abind(list(a=NULL, b=NULL), along=1) NULL > dimnames(abind(list(a=NULL, b=NULL), along=1)) NULL > abind(list(a=NULL, b=NULL), along=2) NULL > dim(abind(list(a=NULL, b=NULL), along=2)) NULL > dimnames(abind(list(a=NULL, b=NULL), along=2)) NULL > abind(list(a=NULL, b=NULL), along=0) NULL > dim(abind(list(a=NULL, b=NULL), along=0)) NULL > dimnames(abind(list(a=NULL, b=NULL), along=0)) NULL > abind(list(a=NULL, b=NULL), along=3) NULL > # behavior with numeric(0) objects > dim(abind(list(a=numeric(0), b=numeric(0)), along=1)) [1] 0 > abind(list(a=numeric(0), b=numeric(0)), along=1) numeric(0) > dimnames(abind(list(a=numeric(0), b=numeric(0)), along=1)) [[1]] NULL > abind(list(a=numeric(0), b=numeric(0)), along=2) a b > dim(abind(list(a=numeric(0), b=numeric(0)), along=2)) [1] 0 2 > dimnames(abind(list(a=numeric(0), b=numeric(0)), along=2)) [[1]] NULL [[2]] [1] "a" "b" > abind(list(a=numeric(0), b=numeric(0)), along=0) a b > dim(abind(list(a=numeric(0), b=numeric(0)), along=0)) [1] 2 0 > dimnames(abind(list(a=numeric(0), b=numeric(0)), along=0)) [[1]] [1] "a" "b" [[2]] NULL > # check hierarchical name construction > abind(x=cbind(1:3,4:6)) x1 x2 [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(x=cbind(a=1:3,b=4:6), hier.names=TRUE) x.a x.b [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(x=cbind(1:3,4:6), hier.names=TRUE) x1 x2 [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(cbind(a=1:3,b=4:6)) a b [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(cbind(1:3,4:6), hier.names=TRUE) [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(cbind(a=1:3,b=4:6), hier.names=TRUE) a b [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(cbind(a=1:3,b=4:6), cbind(a=7:9,b=10:12), hier.names=TRUE) a b a b [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > abind(x=cbind(a=1:3,b=4:6), y=cbind(a=7:9,b=10:12), hier.names=TRUE) x.a x.b y.a y.b [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > abind(x=cbind(1:3,4:6), y=cbind(7:9,10:12), hier.names=TRUE) x1 x2 y1 y2 [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > abind(cbind(1:3,4:6), cbind(7:9,10:12), hier.names=TRUE) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > abind(x=cbind(a=1:3,b=4:6), hier.names='after') a.x b.x [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(x=cbind(a=1:3,b=4:6), hier.names='before') x.a x.b [1,] 1 4 [2,] 2 5 [3,] 3 6 > abind(x=cbind(a=1:3,b=4:6), hier.names='no') a b [1,] 1 4 [2,] 2 5 [3,] 3 6 > > proc.time() user system elapsed 0.15 0.04 0.20