I"ve got a data frame with a selection of columns I want to compute a rank-order correlation matrix from without disturbing the original data frame. foo[,c("a","b","d","f","g")] What I wanted to do, intuitively, was: > cor(rank(foo[,c("a","b","d","f","g")])) but rank in that context ranks all of the values from the matrix together as one long vector. I want the columns (and their names) to be preserved, just replacing the value with its rank. What is the most direct way to get what I want? Thanks! -- Russell Senior ``The two chiefs turned to each other. seniorr at aracnet.com Bellison uncorked a flood of horrible profanity, which, translated meant, `This is extremely unusual.' '' -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>>>> "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 data.frame. 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 set.seed(101) 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) str(fooR) (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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Russell, At 04:52 PM 5/6/2002 -0700, Russell Senior wrote:>I"ve got a data frame with a selection of columns I want to compute a >rank-order correlation matrix from without disturbing the original >data frame. > > foo[,c("a","b","d","f","g")] > >What I wanted to do, intuitively, was: > > > cor(rank(foo[,c("a","b","d","f","g")])) > >but rank in that context ranks all of the values from the matrix >together as one long vector. I want the columns (and their names) to >be preserved, just replacing the value with its rank. What is the >most direct way to get what I want?Try cor(apply(foo[,c("a","b","d","f","g")], 2, rank)) I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Also check out the rcorr function in the Hmisc library if you have very large matrices. rcorr may be a bit faster, and it handles pairwise deletion of missing values. rcorr accepts a matrix as its first argument. -Frank Harrell On Tue, 07 May 2002 10:19:52 -0400 John Fox <jfox at mcmaster.ca> wrote:> Dear Russell, > > At 04:52 PM 5/6/2002 -0700, Russell Senior wrote: > > >I"ve got a data frame with a selection of columns I want to compute a > >rank-order correlation matrix from without disturbing the original > >data frame. > > > > foo[,c("a","b","d","f","g")] > > > >What I wanted to do, intuitively, was: > > > > > cor(rank(foo[,c("a","b","d","f","g")])) > > > >but rank in that context ranks all of the values from the matrix > >together as one long vector. I want the columns (and their names) to > >be preserved, just replacing the value with its rank. What is the > >most direct way to get what I want? > > Try > > cor(apply(foo[,c("a","b","d","f","g")], 2, rank)) > > I hope that this helps, > John > > > > ----------------------------------------------------- > John Fox > Department of Sociology > McMaster University > Hamilton, Ontario, Canada L8S 4M4 > email: jfox at mcmaster.ca > phone: 905-525-9140x23604 > web: www.socsci.mcmaster.ca/jfox > ----------------------------------------------------- > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Frank E Harrell Jr Prof. of Biostatistics & Statistics Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._