Returns the rank of a matrix X
, using the QR decomposition, QR
.
Included here as a simple function, because rank
does something different
and it is not obvious what to use for matrix rank.
Examples
M <- outer(1:3, 3:1)
M
#> [,1] [,2] [,3]
#> [1,] 3 2 1
#> [2,] 6 4 2
#> [3,] 9 6 3
R(M)
#> [1] 1
M <- matrix(1:9, 3, 3)
M
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9
R(M)
#> [1] 2
# why rank=2?
echelon(M)
#> [,1] [,2] [,3]
#> [1,] 1 0 -1
#> [2,] 0 1 2
#> [3,] 0 0 0
set.seed(1234)
M <- matrix(sample(1:9), 3, 3)
M
#> [,1] [,2] [,3]
#> [1,] 6 1 7
#> [2,] 5 8 9
#> [3,] 4 2 3
R(M)
#> [1] 3