Combine multidimensional SparseArray objects
SparseArray-abind.Rd
Like ordinary matrices and arrays in base R, SparseMatrix derivatives
can be combined by rows or columns, with rbind()
or cbind()
,
and multidimensional SparseArray derivatives can be bound along
any dimension with abind()
.
Note that arbind()
can also be used to combine the objects along
their first dimension, and acbind()
can be used to combine them
along their second dimension.
See also
cbind
in base R.abind
in the S4Arrays package.SparseArray objects.
Ordinary array objects in base R.
Examples
## ---------------------------------------------------------------------
## COMBINING SparseMatrix OBJECTS
## ---------------------------------------------------------------------
m1a <- matrix(1:15, nrow=3, ncol=5,
dimnames=list(NULL, paste0("M1y", 1:5)))
m1b <- matrix(101:135, nrow=7, ncol=5,
dimnames=list(paste0("M2x", 1:7), paste0("M2y", 1:5)))
sm1a <- SparseArray(m1a)
sm1b <- SparseArray(m1b)
rbind(sm1a, sm1b)
#> <10 x 5 SparseMatrix> of type "integer" [nzcount=50 (100%)]:
#> M1y1 M1y2 M1y3 M1y4 M1y5
#> 1 4 7 10 13
#> 2 5 8 11 14
#> 3 6 9 12 15
#> M2x1 101 108 115 122 129
#> M2x2 102 109 116 123 130
#> M2x3 103 110 117 124 131
#> M2x4 104 111 118 125 132
#> M2x5 105 112 119 126 133
#> M2x6 106 113 120 127 134
#> M2x7 107 114 121 128 135
## ---------------------------------------------------------------------
## COMBINING SparseArray OBJECTS WITH 3 DIMENSIONS
## ---------------------------------------------------------------------
a2a <- array(1:105, dim=c(5, 7, 3),
dimnames=list(NULL, paste0("A1y", 1:7), NULL))
a2b <- array(1001:1105, dim=c(5, 7, 3),
dimnames=list(paste0("A2x", 1:5), paste0("A2y", 1:7), NULL))
sa2a <- SparseArray(a2a)
sa2b <- SparseArray(a2b)
abind(sa2a, sa2b) # same as 'abind(sa2a, sa2b, along=3)'
#> <5 x 7 x 6 SparseArray> of type "integer" [nzcount=210 (100%)]:
#> ,,1
#> A1y1 A1y2 A1y3 A1y4 A1y5 A1y6 A1y7
#> A2x1 1 6 11 16 21 26 31
#> A2x2 2 7 12 17 22 27 32
#> A2x3 3 8 13 18 23 28 33
#> A2x4 4 9 14 19 24 29 34
#> A2x5 5 10 15 20 25 30 35
#>
#> ...
#>
#> ,,6
#> A1y1 A1y2 A1y3 A1y4 A1y5 A1y6 A1y7
#> A2x1 1071 1076 1081 1086 1091 1096 1101
#> A2x2 1072 1077 1082 1087 1092 1097 1102
#> A2x3 1073 1078 1083 1088 1093 1098 1103
#> A2x4 1074 1079 1084 1089 1094 1099 1104
#> A2x5 1075 1080 1085 1090 1095 1100 1105
#>
abind(sa2a, sa2b, rev.along=0) # same as 'abind(sa2a, sa2b, along=4)'
#> <5 x 7 x 3 x 2 SparseArray> of type "integer" [nzcount=210 (100%)]:
#> ,,1,1
#> A1y1 A1y2 A1y3 A1y4 A1y5 A1y6 A1y7
#> A2x1 1 6 11 16 21 26 31
#> A2x2 2 7 12 17 22 27 32
#> A2x3 3 8 13 18 23 28 33
#> A2x4 4 9 14 19 24 29 34
#> A2x5 5 10 15 20 25 30 35
#>
#> ...
#>
#> ,,3,2
#> A1y1 A1y2 A1y3 A1y4 A1y5 A1y6 A1y7
#> A2x1 1071 1076 1081 1086 1091 1096 1101
#> A2x2 1072 1077 1082 1087 1092 1097 1102
#> A2x3 1073 1078 1083 1088 1093 1098 1103
#> A2x4 1074 1079 1084 1089 1094 1099 1104
#> A2x5 1075 1080 1085 1090 1095 1100 1105
#>
a3a <- array(1:60, dim=c(3, 5, 4),
dimnames=list(NULL, paste0("A1y", 1:5), NULL))
a3b <- array(101:240, dim=c(7, 5, 4),
dimnames=list(paste0("A2x", 1:7), paste0("A2y", 1:5), NULL))
sa3a <- SparseArray(a3a)
sa3b <- SparseArray(a3b)
arbind(sa3a, sa3b) # same as 'abind(sa3a, sa3b, along=1)'
#> <10 x 5 x 4 SparseArray> of type "integer" [nzcount=200 (100%)]:
#> ,,1
#> A1y1 A1y2 A1y3 A1y4 A1y5
#> 1 4 7 10 13
#> 2 5 8 11 14
#> ... . . . . .
#> A2x6 106 113 120 127 134
#> A2x7 107 114 121 128 135
#>
#> ...
#>
#> ,,4
#> A1y1 A1y2 A1y3 A1y4 A1y5
#> 46 49 52 55 58
#> 47 50 53 56 59
#> ... . . . . .
#> A2x6 211 218 225 232 239
#> A2x7 212 219 226 233 240
#>
## ---------------------------------------------------------------------
## Sanity checks
## ---------------------------------------------------------------------
sm1 <- rbind(sm1a, sm1b)
m1 <- rbind(m1a, m1b)
stopifnot(identical(as.array(sm1), m1), identical(sm1, SparseArray(m1)))
sa2 <- abind(sa2a, sa2b)
stopifnot(identical(sa2, abind(sa2a, sa2b, along=3)))
a2 <- abind(a2a, a2b, along=3)
stopifnot(identical(as.array(sa2), a2), identical(sa2, SparseArray(a2)))
sa2 <- abind(sa2a, sa2b, rev.along=0)
stopifnot(identical(sa2, abind(sa2a, sa2b, along=4)))
a2 <- abind(a2a, a2b, along=4)
stopifnot(identical(as.array(sa2), a2), identical(sa2, SparseArray(a2)))
sa3 <- arbind(sa3a, sa3b)
a3 <- arbind(a3a, a3b)
stopifnot(identical(as.array(sa3), a3), identical(sa3, SparseArray(a3)))