[R] sort dates within a factor
xavier.chardon at free.fr
xavier.chardon at free.fr
Tue Sep 29 12:12:45 CEST 2009
Is this what you want?
test[order(test$nr, -as.integer(test$date)),]
Xavier
----- Mail Original -----
De: "Stefan Uhmann" <stefan.uhmann at googlemail.com>
À: r-help at r-project.org
Envoyé: Mardi 29 Septembre 2009 11h27:20 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne
Objet: [R] sort dates within a factor
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
______________________________________________
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