Hi,

late, but here is the feedback.
plyr was the way to go, but given my large df, plyr crashed when I
applied it to the whole df. So I just took the part of the df to sort,
applied plyr to it and then merged it with the original df.

Thank you, Xavier & Hadley!
Stefan

xavier.char...@free.fr schrieb, Am 29.09.2009 13:58:
Apologies for the misunderstanding. I can come up with a solution that might 
suit your needs:

library(plyr)
out <- ddply(test, .(nr), function(x) data.frame(date=x$date, 
index=rank(-as.integer(x$date))))
out[is.na(out$nr) | is.na(out$date), "index"] <- NA

Xavier

----- Mail Original -----
De: "Stefan Uhmann" <stefan.uhm...@googlemail.com>
À: r-help@r-project.org
Envoyé: Mardi 29 Septembre 2009 13h15:10 GMT +01:00 Amsterdam / Berlin / Berne 
/ Rome / Stockholm / Vienne
Objet: Re: [R] sort dates within a factor

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.char...@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.uhm...@googlemail.com>
À: r-help@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@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.

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to