Skip to contents

Like ordinary arrays in base R, SparseArray derivatives support subassignment via the [<- operator.

See also

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
#> 

svt[5:3, c(4,2,4), 2:3] <- -99L

## Sanity checks:
a[5:3, c(4,2,4), 2:3] <- -99L
stopifnot(identical(as.array(svt), a), identical(svt, SparseArray(a)))