[R] Sorting a date vector
David Winsemius
dwinsemius at comcast.net
Tue Dec 16 14:35:12 CET 2008
You cannot keep them as strings and still get the benefits of working
with date-class objects. You should read more documentation regarding
dates. The as.Date function turns strings into a form that is stored
internally as number of days since some reference date and what you
are seeing is the default display format, %Y-%m-%d". Learn how to use
the output formats so that you see what you desire.
?as.Date
?Dates
?format.Date
--
David Winsemius
On Dec 16, 2008, at 8:24 AM, RON70 wrote:
>
> Yes you are right. However using that code, format of date is
> altered. I need
> to main same format as the input data i.e. "10-02-2008" not
> "2008-10-02",
> still having date-class. Any better idea?
>
>
> David Winsemius wrote:
>>
>> You might want to look at your date format more closely. Both the
>> separator and the year format specs fail to match your input.
>>
>>> as.Date("10-02-2008", format = "%m/%d/%y")
>> [1] NA
>>> as.Date("10-02-2008", format = "%m-%d-%Y")
>> [1] "2008-10-02"
>>
>> --
>> David Winsemius
>> On Dec 16, 2008, at 7:54 AM, 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.
>>>
>>> Can anyone please tell me what would be correct approach?
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Sorting-a-date-vector-tp21032540p21032540.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>
>> ______________________________________________
>> 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.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Sorting-a-date-vector-tp21032540p21032997.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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