[R] Sorting a date vector

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Dec 16 14:16:31 CET 2008


On Tue, 16 Dec 2008, RON70 wrote:

>
> I have a date-like-vector like :
>
>> date_file
> "10-02-2008" "10-03-2008" "10-06-2008" "10-07-2008" "10-09-2008"
> "10-10-2008" "10-13-2008" "10-14-2008" "10-15-2008"
> "10-16-2008" "10-17-2008" "10-20-2008" "10-21-2008" "10-22-2008"
> "10-23-2008" "10-24-2008" "10-28-2008" "10-29-2008"
> "10-30-2008" "10-31-2008" "11-03-2008" "11-04-2008" "11-05-2008"
> "11-06-2008" "11-07-2008" "11-10-2008" "11-11-2008"
> "11-12-2008" "11-13-2008" "11-14-2008" "11-17-2008" "11-18-2008"
> "11-19-2008" "11-20-2008" "11-21-2008" "11-24-2008"
> "11-25-2008" "11-26-2008" "11-28-2008" "12-01-2008" "12-02-2008"
> "12-03-2008" "12-04-2008" "12-05-2008" "12-08-2008"
> "12-09-2008" "12-10-2008" "12-11-2008" "12-12-2008" "12-15-2008"
> "4-18-2008"  "4-21-2008"  "4-22-2008"  "4-23-2008"
> "4-24-2008"  "4-28-2008"  "4-29-2008"  "5-01-2008"  "5-05-2008"
> "5-06-2008"  "5-07-2008"  "5-09-2008"  "5-12-2008"
> "5-13-2008"  "5-14-2008"  "5-15-2008"  "5-16-2008"  "5-19-2008"
> "5-20-2008"  "5-21-2008"  "5-22-2008"  "5-23-2008"
> "5-27-2008"  "5-28-2008"  "5-29-2008"  "5-30-2008"  "6-02-2008"
> "6-03-2008"  "6-05-2008"  "6-06-2008"  "6-09-2008"
> "6-10-2008"  "6-11-2008"  "6-12-2008"  "6-13-2008"  "6-17-2008"
> "6-18-2008"  "6-19-2008"  "6-20-2008"  "6-23-2008"
> "6-24-2008"  "6-25-2008"  "6-26-2008"  "6-27-2008"  "7-01-2008"
> "7-02-2008"  "7-04-2008"  "7-07-2008"  "7-08-2008"
> "7-09-2008"  "7-10-2008"  "7-11-2008"  "7-15-2008"  "7-16-2008"
> "7-18-2008"  "7-21-2008"  "7-22-2008"  "7-23-2008"
> "7-24-2008"  "7-25-2008"  "7-28-2008"  "7-30-2008"  "7-31-2008"
> "8-01-2008"  "8-04-2008"  "8-05-2008"  "8-06-2008"
> "8-07-2008"  "8-08-2008"  "8-11-2008"  "8-12-2008"  "8-13-2008"
> "8-15-2008"  "8-18-2008"  "8-19-2008"  "8-20-2008"
> "8-21-2008"  "8-22-2008"  "8-25-2008"  "8-26-2008"  "8-27-2008"
> "8-28-2008"  "8-29-2008"  "9-03-2008"  "9-04-2008"
> "9-05-2008"  "9-08-2008"  "9-09-2008"  "9-10-2008"  "9-11-2008"
> "9-12-2008"  "9-15-2008"  "9-16-2008"  "9-17-2008"
> "9-18-2008"  "9-19-2008"  "9-22-2008"  "9-23-2008"  "9-24-2008"
> "9-25-2008"  "9-26-2008"  "9-29-2008"  "9-30-2008"
>
> I wanted to sort this in ascending order. I tried using simply sort()
> function, without altering the format of date, but it didnot work. Next I
> tried to convert that vector in a date-class vector so that, I could sort
> them but in vein :(
>
> I used :
> as.Date(date_file, format="%m/%d/%y")
>
> However it did not work.

Your separator is '-' not '/', and you have 4-figure dates.  Looks like

sort(as.Date(date_file, format="%m-%d-%Y"))

is what you intended.


> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Please do, and remember to be more helpful than 'it did not work'!


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list