[R] sort dates within a factor

Stefan Uhmann stefan.uhmann at googlemail.com
Tue Sep 29 11:27:20 CEST 2009


Dear List,

I have the following data:

 >>
test <- data.frame(date = 
as.Date(c('2007-01-01','2008-03-24','2003-03-02','2008-05-03','2002-05-23','2001-06-30','2005-12-04')),
	nr = c(2000,2000,2000,2001,2002,2003,2003))
test
         date   nr
1 2007-01-01 2000
2 2008-03-24 2000
3 2003-03-02 2000
4 2008-05-03 2001
5 2002-05-23 2002
6 2001-06-30 2003
7 2005-12-04 2003
<<

and would like a result which looks as follows, i.e. that sorts the 
dates within the factor starting with the last one:

 >>
cbind(test, index = c(2,1,3,1,1,2,1))
<<

         date   nr index
1 2007-01-01 2000     2
2 2008-03-24 2000     1
3 2003-03-02 2000     3
4 2008-05-03 2001     1
5 2002-05-23 2002     1
6 2001-06-30 2003     2
7 2005-12-04 2003     1

I've done sth. similar before, but I have difficulties to figure it out 
this time.

Thanks for your help in advance!

Regards,
Stefan




More information about the R-help mailing list