[R] sort dates within a factor

Stefan Uhmann stefan.uhmann at googlemail.com
Tue Sep 29 13:15:10 CEST 2009


Hi Xavier,

thank you for your suggestion, it's not exactly what I need. However I 
made some progress using tapply, but it does not give me the 'correct' 
result (see index - that's what I want to obtain!) when NAs are involved:

test <- data.frame(date =
as.Date(c('2007-01-01','2008-03-24','NA','2003-03-02','2004-01-01','2008-05-03','2002-05-23','2001-06-30','2005-12-04')),
	nr = c(2000,2000,2000,2000,NA,2001,2002,2003,2003))
test
# the result should look like:
cbind(test, index = c(2,1,NA,3,NA,1,1,2,1))
# tapply does not treat NAs as intended
unlist(tapply(test$date, test$nr, function(x){order(x, decreasing = T)}))

Kind Regards,
Stefan

xavier.chardon at free.fr schrieb, Am 29.09.2009 12:12:
> 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