Methods for "[", i.e., extraction or subsetting mostly of matrices, in package Matrix.

Methods

There are more than these:

x = "Matrix", i = "missing", j = "missing", drop= "ANY"

...

x = "Matrix", i = "numeric", j = "missing", drop= "missing"

...

x = "Matrix", i = "missing", j = "numeric", drop= "missing"

...

x = "dsparseMatrix", i = "missing", j = "numeric", drop= "logical"

...

x = "dsparseMatrix", i = "numeric", j = "missing", drop= "logical"

...

x = "dsparseMatrix", i = "numeric", j = "numeric", drop= "logical"

...

See also

[<–methods for subassignment to "Matrix" objects. Extract about the standard extraction.

Examples


str(m <- Matrix(round(rnorm(7*4),2), nrow = 7))
#> Formal class 'dgeMatrix' [package "Matrix"] with 4 slots
#>   ..@ Dim     : int [1:2] 7 4
#>   ..@ Dimnames:List of 2
#>   .. ..$ : NULL
#>   .. ..$ : NULL
#>   ..@ x       : num [1:28] -0.06 -0.16 -1.47 -0.48 0.42 1.36 -0.1 0.39 -0.05 -1.38 ...
#>   ..@ factors : list()
stopifnot(identical(m, m[]))
m[2, 3]   # simple number
#> [1] -0.16
m[2, 3:4] # simple numeric of length 2
#> [1] -0.16  0.77
m[2, 3:4, drop=FALSE] # sub matrix of class 'dgeMatrix'
#> 1 x 2 Matrix of class "dgeMatrix"
#>       [,1] [,2]
#> [1,] -0.16 0.77
## rows or columns only:
m[1,]     # first row, as simple numeric vector
#> [1] -0.06  0.39  0.76  0.36
m[,1:2]   # sub matrix of first two columns
#> 7 x 2 Matrix of class "dgeMatrix"
#>       [,1]  [,2]
#> [1,] -0.06  0.39
#> [2,] -0.16 -0.05
#> [3,] -1.47 -1.38
#> [4,] -0.48 -0.41
#> [5,]  0.42 -0.39
#> [6,]  1.36 -0.06
#> [7,] -0.10  1.10

showMethods("[", inherited = FALSE)
#> Function: [ (package base)
#> x="Matrix", i="ANY", j="NULL", drop="ANY"
#> x="Matrix", i="NULL", j="ANY", drop="ANY"
#> x="Matrix", i="NULL", j="NULL", drop="ANY"
#> x="Matrix", i="index", j="index", drop="logical"
#> x="Matrix", i="index", j="index", drop="missing"
#> x="Matrix", i="index", j="missing", drop="logical"
#> x="Matrix", i="index", j="missing", drop="missing"
#> x="Matrix", i="lMatrix", j="missing", drop="missing"
#> x="Matrix", i="matrix", j="missing", drop="missing"
#> x="Matrix", i="missing", j="index", drop="logical"
#> x="Matrix", i="missing", j="index", drop="missing"
#> x="Matrix", i="missing", j="missing", drop="logical"
#> x="Matrix", i="missing", j="missing", drop="missing"
#> x="Matrix", i="nMatrix", j="missing", drop="missing"
#> x="abIndex", i="index", j="ANY", drop="ANY"
#> x="nonStructure", i="ANY", j="ANY", drop="ANY"
#> x="sparseVector", i="NULL", j="ANY", drop="ANY"
#> x="sparseVector", i="index", j="missing", drop="missing"
#> x="sparseVector", i="lsparseVector", j="missing", drop="missing"
#> x="sparseVector", i="missing", j="missing", drop="missing"
#> x="sparseVector", i="nsparseVector", j="missing", drop="missing"
#>