Subsetting a SparseArray object
SparseArray-subsetting.Rd
Like ordinary arrays in base R, SparseArray derivatives support
subsetting via the single bracket operator ([
).
See also
drop
in base R to drop the ineffective dimensions of an array or array-like object.Lindex2Mindex
in the S4Arrays package for how to convert an L-index to an M-index and vice-versa.SparseArray objects.
Examples
a <- array(0L, dim=5:3)
a[c(1:2, 8, 10, 15:17, 20, 24, 40, 56:60)] <- (1:15)*10L
svt <- SparseArray(a)
svt
#> <5 x 4 x 3 SparseArray> of type "integer" [nzcount=15 (25%)]:
#> ,,1
#> [,1] [,2] [,3] [,4]
#> [1,] 10 0 0 60
#> [2,] 20 0 0 70
#> [3,] 0 30 0 0
#> [4,] 0 0 0 0
#> [5,] 0 40 50 80
#>
#> ,,2
#> [,1] [,2] [,3] [,4]
#> [1,] 0 0 0 0
#> [2,] 0 0 0 0
#> [3,] 0 0 0 0
#> [4,] 90 0 0 0
#> [5,] 0 0 0 100
#>
#> ,,3
#> [,1] [,2] [,3] [,4]
#> [1,] 0 0 0 110
#> [2,] 0 0 0 120
#> [3,] 0 0 0 130
#> [4,] 0 0 0 140
#> [5,] 0 0 0 150
#>
## ---------------------------------------------------------------------
## N-dimensional subsetting
## ---------------------------------------------------------------------
svt[5:3, c(4,2,4), 2:3]
#> <3 x 3 x 2 SparseArray> of type "integer" [nzcount=8 (44%)]:
#> ,,1
#> [,1] [,2] [,3]
#> [1,] 100 0 100
#> [2,] 0 0 0
#> [3,] 0 0 0
#>
#> ,,2
#> [,1] [,2] [,3]
#> [1,] 150 0 150
#> [2,] 140 0 140
#> [3,] 130 0 130
#>
svt[ , c(4,2,4), 2:3]
#> <5 x 3 x 2 SparseArray> of type "integer" [nzcount=12 (40%)]:
#> ,,1
#> [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#> [4,] 0 0 0
#> [5,] 100 0 100
#>
#> ,,2
#> [,1] [,2] [,3]
#> [1,] 110 0 110
#> [2,] 120 0 120
#> [3,] 130 0 130
#> [4,] 140 0 140
#> [5,] 150 0 150
#>
svt[ , c(4,2,4), -1]
#> <5 x 3 x 2 SparseArray> of type "integer" [nzcount=12 (40%)]:
#> ,,1
#> [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#> [4,] 0 0 0
#> [5,] 100 0 100
#>
#> ,,2
#> [,1] [,2] [,3]
#> [1,] 110 0 110
#> [2,] 120 0 120
#> [3,] 130 0 130
#> [4,] 140 0 140
#> [5,] 150 0 150
#>
svt[ , c(4,2,4), 1]
#> <5 x 3 SparseMatrix> of type "integer" [nzcount=8 (53%)]:
#> [,1] [,2] [,3]
#> [1,] 60 0 60
#> [2,] 70 0 70
#> [3,] 0 30 0
#> [4,] 0 0 0
#> [5,] 80 40 80
svt2 <- svt[ , c(4,2,4), 1, drop=FALSE]
svt2
#> <5 x 3 x 1 SparseArray> of type "integer" [nzcount=8 (53%)]:
#> ,,1
#> [,1] [,2] [,3]
#> [1,] 60 0 60
#> [2,] 70 0 70
#> [3,] 0 30 0
#> [4,] 0 0 0
#> [5,] 80 40 80
#>
## Ineffective dimensions can always be dropped as a separate step:
drop(svt2)
#> <5 x 3 SparseMatrix> of type "integer" [nzcount=8 (53%)]:
#> [,1] [,2] [,3]
#> [1,] 60 0 60
#> [2,] 70 0 70
#> [3,] 0 30 0
#> [4,] 0 0 0
#> [5,] 80 40 80
svt[ , c(4,2,4), integer(0)]
#> <5 x 3 x 0 SparseArray> of type "integer" [nzcount=0 (NaN%)]
dimnames(a) <- list(letters[1:5], NULL, LETTERS[1:3])
svt <- SparseArray(a)
svt[c("d", "a"), c(4,2,4), "C"]
#> <2 x 3 SparseMatrix> of type "integer" [nzcount=4 (67%)]:
#> [,1] [,2] [,3]
#> d 140 0 140
#> a 110 0 110
svt2 <- svt["e", c(4,2,4), , drop=FALSE]
svt2
#> <1 x 3 x 3 SparseArray> of type "integer" [nzcount=7 (78%)]:
#> ,,A
#> [,1] [,2] [,3]
#> e 80 40 80
#>
#> ,,B
#> [,1] [,2] [,3]
#> e 100 0 100
#>
#> ,,C
#> [,1] [,2] [,3]
#> e 150 0 150
#>
drop(svt2)
#> <3 x 3 SparseMatrix> of type "integer" [nzcount=7 (78%)]:
#> A B C
#> [1,] 80 100 150
#> [2,] 40 0 0
#> [3,] 80 100 150
## ---------------------------------------------------------------------
## 1D-style subsetting (a.k.a. linear subsetting)
## ---------------------------------------------------------------------
## Using a numeric vector (L-index):
svt[c(60, 24, 21, 56)]
#> [1] 150 90 0 110
## Using a matrix subscript (M-index):
m <- rbind(c(5, 4, 3),
c(4, 1, 2),
c(1, 1, 2),
c(1, 4, 3))
svt[m]
#> [1] 150 90 0 110
## See '?Lindex2Mindex' in the S4Arrays package for how to convert an
## L-index to an M-index and vice-versa.
## ---------------------------------------------------------------------
## Sanity checks
## ---------------------------------------------------------------------
svt2 <- svt[5:3, c(4,2,4), 2:3]
a2 <- a [5:3, c(4,2,4), 2:3]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- svt[ , c(4,2,4), 2:3]
a2 <- a [ , c(4,2,4), 2:3]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- svt[ , c(4,2,4), -1]
a2 <- a [ , c(4,2,4), -1]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- svt[ , c(4,2,4), 1]
a2 <- a [ , c(4,2,4), 1]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- svt[ , c(4,2,4), 1, drop=FALSE]
a2 <- a [ , c(4,2,4), 1, drop=FALSE]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- drop(svt2)
a2 <- drop(a2)
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- svt[ , c(4,2,4), integer(0)]
a2 <- a [ , c(4,2,4), integer(0)]
stopifnot(identical(as.array(svt2), a2),
identical(unname(svt2), unname(SparseArray(a2))))
svt2 <- svt[c("d", "a"), c(4,2,4), "C"]
a2 <- a [c("d", "a"), c(4,2,4), "C"]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- svt["e", c(4,2,4), , drop=FALSE]
a2 <- a ["e", c(4,2,4), , drop=FALSE]
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
svt2 <- drop(svt2)
a2 <- drop(a2)
stopifnot(identical(as.array(svt2), a2), identical(svt2, SparseArray(a2)))
stopifnot(identical(svt[c(60, 24, 21, 56)], svt[m]))