[R] sorting dataframe by arbitrary order

William Dunlap wdunlap at tibco.com
Fri Oct 14 21:50:16 CEST 2011


Set the levels of the factor a$V1 to the order
in which you want them to be sorted.  E.g.,

  > a <- data.frame(V1=letters[rep(4:1,2)], V2=1001:1008)
  > a[do.call(order,a[c('V1','V2')]),]
    V1   V2
  4  a 1004
  8  a 1008
  3  b 1003
  7  b 1007
  2  c 1002
  6  c 1006
  1  d 1001
  5  d 1005
  > a$V1 <- factor(a$V1, levels=c("a","c","d","b"))
  > a[do.call(order,a[c('V1','V2')]),]
    V1   V2
  4  a 1004
  8  a 1008
  2  c 1002
  6  c 1006
  1  d 1001
  5  d 1005
  3  b 1003
  7  b 1007

This means that tables and plots will be ordered in
the way as well.  E.g.,

  > with(a, table(V1, V2))
     V2
  V1  1001 1002 1003 1004 1005 1006 1007 1008
    a    0    0    0    1    0    0    0    1
    c    0    1    0    0    0    1    0    0
    d    1    0    0    0    1    0    0    0
    b    0    0    1    0    0    0    1    0


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Trevor Davies
> Sent: Friday, October 14, 2011 12:05 PM
> To: r-help at r-project.org
> Subject: [R] sorting dataframe by arbitrary order
> 
> This has been dogging me for a while. I've started making a lot of tables
> via xtable so the way I want to sort things is not always in alphabetical or
> numerical order.
> 
> As an example, consider I have a dataframe as follows
> 
> set.seed(100)
> a <- data.frame(V1=sample(letters[1:4],100, replace=T),V2=1:100)
> 
> I know I can sort the columns first by V1 first and then by V2 by:
> 
> sorted.a <- a[do.call(order,a[c('V1','V2')]),]
> 
> What I want to do is exactly that but I do not want V1 sorted
> alphabetically.  Rather, I would like it sorted as 'a','c','d','b'.
> 
> I know I could do it with a subset, rbind function but I thought there may
> be a more elegant way?
> 
> Thanks for the help.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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