[R] rank/sort problem

David Winsemius dwinsemius at comcast.net
Sat Nov 21 18:59:51 CET 2015


> On Nov 21, 2015, at 4:44 AM, Axel Urbiz <axel.urbiz at gmail.com> wrote:
> 
> Hello,
> 
> I would like to sort the df below, such that it sorts y1 in decreasing order for tt == 1 and in increasing order for 
> tt == 0. My solution is below, but curious if there might be something better (meaning faster in this case). 
> 
> Actually, if instead if implicitly sorting, I could add a variable ‘rank’ as in my ‘hope’ data frame below, that would work too (as I just need the ranking of observations, not necessarily the data explicitly sorted in the results).
> 
> ### Sample data
> y1 <- c(50,100,200,20,400,100,500,1000,12,25)
> tt <- factor(c(1,1,1,0,0,0,0,1,0,0))
> df <- data.frame(y1, tt)
> 
> ### My solution
> library(dplyr)
> sorted <- rbind(df[df$tt == 1, ] %>% arrange(desc(y1)),
>                df[df$tt == 0, ] %>% arrange(y1))
> 
> ### What I hope to get
> hope <- data.frame(df,
>                   rank = c(4, 3, 2, 6, 9, 8, 10, 1, 5, 7))
> hope[order(hope$rank),]

Couldn’t this just be accomplished with a bit of Boolean arithmetic:

  df[ with(df,order( c(1,-1)[ 1+(tt==1) ]*y1 ) ), ] 


This inverts the y1-ordering of (tt==1) cases. If you need that third column, then cbind the “rank" vector with seq(nrow(df)) but I would avoid using the name “rank" for obvious reasons. (And note that `df` is also an R function name.) 


> 
> 
> Thanks for any help.
> 
> Best Axel.
> 	[[alternative HTML version deleted]]

Please. This is a plain-text list.  

> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list