# [R] Rank of a matrix?

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Jun 7 10:11:09 CEST 2002

```On 7 Jun 2002, Peter Dalgaard BSA wrote:

> Manuel Castejón Limas <manuel.castejon at dim.unirioja.es> writes:
>
> > Hello everyone,
> > this is a very simple question but I could  not find the answer. :-(
> >
> > Is there any function in R for calculating the rank of a matrix?
> > (In the sense of the dimension of the vectorial space that spans)
>
> It's a byproduct of chol() and qr(), but beware that it is numerically
> ill-defined due to round-off. Notice the tol= argument to qr().

For a more reliable answer, look at the SVD (function svd) and look at the
singular values.  For example (from lda.default)

X.s <- svd(X, nu = 0)
rank <- sum(X.s\$d > tol * X.s\$d[1])

where tol is set to 1e-4 in that application.  If you only want the
rank, set nv=0 in the call too.

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```