Suppose I have two columns, x,y. I can use order(x,y) to calculate a permutation that puts them into increasing order of x, with ties broken by y. I'd like instead to calculate the rank of each pair under the same ordering, but the rank() function doesn't take multiple values as input. Is there a simple way to get what I want? E.g. > x <- c(1,2,3,4,1,2,3,4) > y <- c(1,2,3,1,2,3,1,2) > rank(x+y/10) [1] 1 3 6 7 2 4 5 8 gives me the answer I want, but only because I know the range of y and the size of gaps in the x values. What do I do in general? Duncan Murdoch
Dear Duncan, How about something like the following? rank2 <- function(x, y){ x <- (x - min(x))/diff(range(x)) y <- (y - min(y))/diff(range(y)) rank(10*(x + 1) + y) } Regards, John On Wed, 21 Jun 2006 13:43:39 -0400 Duncan Murdoch <murdoch at stats.uwo.ca> wrote:> Suppose I have two columns, x,y. I can use order(x,y) to calculate a > > permutation that puts them into increasing order of x, > with ties broken by y. > > I'd like instead to calculate the rank of each pair under the same > ordering, but the rank() function doesn't take multiple values > as input. Is there a simple way to get what I want? > > E.g. > > > x <- c(1,2,3,4,1,2,3,4) > > y <- c(1,2,3,1,2,3,1,2) > > rank(x+y/10) > [1] 1 3 6 7 2 4 5 8 > > gives me the answer I want, but only because I know the range of y > and > the size of gaps in the x values. What do I do in general? > > Duncan Murdoch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
This might work for you:> order(order(x, y))[1] 1 3 6 7 2 4 5 8 Andy From: Duncan Murdoch> > Suppose I have two columns, x,y. I can use order(x,y) to > calculate a permutation that puts them into increasing order > of x, with ties broken by y. > > I'd like instead to calculate the rank of each pair under the > same ordering, but the rank() function doesn't take multiple > values as input. Is there a simple way to get what I want? > > E.g. > > > x <- c(1,2,3,4,1,2,3,4) > > y <- c(1,2,3,1,2,3,1,2) > > rank(x+y/10) > [1] 1 3 6 7 2 4 5 8 > > gives me the answer I want, but only because I know the range > of y and the size of gaps in the x values. What do I do in general? > > Duncan Murdoch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Duncan Murdoch <murdoch at stats.uwo.ca> writes:> Suppose I have two columns, x,y. I can use order(x,y) to calculate a > permutation that puts them into increasing order of x, > with ties broken by y. > > I'd like instead to calculate the rank of each pair under the same > ordering, but the rank() function doesn't take multiple values > as input. Is there a simple way to get what I want? > > E.g. > > > x <- c(1,2,3,4,1,2,3,4) > > y <- c(1,2,3,1,2,3,1,2) > > rank(x+y/10) > [1] 1 3 6 7 2 4 5 8 > > gives me the answer I want, but only because I know the range of y and > the size of gaps in the x values. What do I do in general?Still not quite general, but in the absence of ties:> z[order(x,y)]<-1:8 > z[1] 1 3 6 7 2 4 5 8 -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Try this: order(order(x,y)) On 6/21/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:> Suppose I have two columns, x,y. I can use order(x,y) to calculate a > permutation that puts them into increasing order of x, > with ties broken by y. > > I'd like instead to calculate the rank of each pair under the same > ordering, but the rank() function doesn't take multiple values > as input. Is there a simple way to get what I want? > > E.g. > > > x <- c(1,2,3,4,1,2,3,4) > > y <- c(1,2,3,1,2,3,1,2) > > rank(x+y/10) > [1] 1 3 6 7 2 4 5 8 > > gives me the answer I want, but only because I know the range of y and > the size of gaps in the x values. What do I do in general? > > Duncan Murdoch > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of Duncan Murdoch > Sent: Wednesday, June 21, 2006 11:14 AM > To: Peter Dalgaard > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] rank(x,y)? > > Peter Dalgaard wrote: > > Duncan Murdoch <murdoch at stats.uwo.ca> writes: > > > > > >> Suppose I have two columns, x,y. I can use order(x,y) to calculate a > >> permutation that puts them into increasing order of x, > >> with ties broken by y. > >> > >> I'd like instead to calculate the rank of each pair under the same > >> ordering, but the rank() function doesn't take multiple values > >> as input. Is there a simple way to get what I want? > >> > >> E.g. > >> > >> > x <- c(1,2,3,4,1,2,3,4) > >> > y <- c(1,2,3,1,2,3,1,2) > >> > rank(x+y/10) > >> [1] 1 3 6 7 2 4 5 8 > >> > >> gives me the answer I want, but only because I know the range of y and > >> the size of gaps in the x values. What do I do in general? > >> > > > > Still not quite general, but in the absence of ties: > > > > > >> z[order(x,y)]<-1:8 > >> z > >> > > [1] 1 3 6 7 2 4 5 8 > > > > > > Thanks to all who have replied. Unfortunately for me, ties do exist, > and I'd like them to get identical ranks. John Fox's suggestion would > handle ties properly, but I'm worried about rounding error giving > spurious ties. > > Duncan Murdoch >Duncan, Similar to John's approach, how about something like rankPair<-function(a,b){ n<-ceiling(log(length(a)))+1 rank(10^n*rank(a)+rank(b)) } This should avoid rounding problems and should be reasonably general (as long as the number of pairs to be ranked doesn't become too large. Dan Daniel J. Nordlund Research and Data Analysis Washington State Department of Social and Health Services Olympia, WA 98504-5204