[R] Non-standard sorts on vectors

William Dunlap wdunlap at tibco.com
Fri Aug 27 02:13:25 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of chipmaney
> Sent: Thursday, August 26, 2010 3:00 PM
> To: r-help at r-project.org
> Subject: [R] Non-standard sorts on vectors
> 
> 
> I have a dataset I need to sort: 
> 
> test.df<-data.frame(Zone=c("Floodplain", "Lake",
> "Shoreline"),Cover=c(50,60,70))
> 
> However, I don't want it sorted ascending/descending but in 
> an order that I
> define via a vector:  
> 
> sort.v<-data.frame(c("Lake","Shoreline","Floodplain"))
> 
> I realize I could probably just create the vector of 
> [factors] in the order
> I want, then assign the value:
> 
> sort.v$Cover<-test.df$Cover
> 
> However, this is inefficient.  

What is your measure of inefficiency?

If you always want results in this order
I would recommend you convert the Cover column
to a factor with the levels in the order that you
want.  E.g.,

> test.df <- data.frame(
 
Zone=c("Floodplain","Lake","Floodplain","Shoreline","Lake","Shoreline"),
    Cover=c(45, 65, 49, 75, 67, 77),
    Year=c(1977, 1980, 1990, 1985, 1995, 2010)) 
> test.df$Zone <- factor(test.df$Zone,
levels=c("Lake","Shoreline","Floodplain"))
> # that call to factor could have been in the original call to
> # to data.frame(), but if you made the data.frame with read.table()
> # then it is easiest to make the factors in a separate step.
> test.df
        Zone Cover Year
1 Floodplain    45 1977
2       Lake    65 1980
3 Floodplain    49 1990
4  Shoreline    75 1985
5       Lake    67 1995
6  Shoreline    77 2010

Now it sorts the way you want and tables, model summaries,
plots, and lots of other things will be in the order
that you like:
  > test.df[order(test.df$Zone),]
          Zone Cover Year
  2       Lake    65 1980
  5       Lake    67 1995
  4  Shoreline    75 1985
  6  Shoreline    77 2010
  1 Floodplain    45 1977
  3 Floodplain    49 1990
  > table(test.df$Zone)

        Lake  Shoreline Floodplain 
           2          2          2 
  > lm(data=test.df, Cover ~ Zone - 1)

  Call:
  lm(formula = Cover ~ Zone - 1, data = test.df)

  Coefficients:
        ZoneLake   ZoneShoreline  ZoneFloodplain  
              66              76              47  
  > library(lattice)
  > xyplot(data=test.df, Cover ~ Year | Zone) 

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> Is there source code or a 
> syntactical trick
> that will do the same more efficiently?  Note: I have 
> investigated order()
> and sort(), but neither seems to have an argument for 
> defining the sorting
> order.
> 
> Thanks in advance, Chipper
> 
> -- 
> View this message in context: 
> http://r.789695.n4.nabble.com/Non-standard-sorts-on-vectors-tp
2340431p2340431.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
> 



More information about the R-help mailing list