[R] Spearman rank-order correlation matrix

Martin Maechler maechler at stat.math.ethz.ch
Tue May 7 15:44:47 CEST 2002

>>>>> "Russell" == Russell Senior <seniorr at aracnet.com> writes:

    Russell> I"ve got a data frame with a selection of columns I
    Russell> want to compute a rank-order correlation matrix
    Russell> from without disturbing the original data frame.

    Russell>   foo[,c("a","b","d","f","g")]

    Russell> What I wanted to do, intuitively, was:

    >> cor(rank(foo[,c("a","b","d","f","g")]))

    Russell> but rank in that context ranks all of the values
    Russell> from the matrix together as one long vector. 

Yes, this is true for almost all R functions.
You have to  *apply*  such functions to a matrix, list or

    Russell> want the columns (and their names) to be preserved,
    Russell> just replacing the value with its rank.  What is
    Russell> the most direct way to get what I want?

In your case, when foo[,] is a data.frame, I'd use sapply.
Here is an example:

## Construct sample data
foo <- data.frame(matrix(rnorm(100*10), 100,10))
names(foo) <- letters[1:10]
summary(foo) # variables named a,b, .., j

## Rank Correlation of some of the variables :
fooR <- sapply(foo, rank)
(V <- cor(fooR[, c("a","b","d","f","g")]))

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
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

More information about the R-help mailing list