[R] How to get the day of the year

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Oct 1 13:24:16 CEST 2008


On 01-Oct-08 10:44:10, Richard.Cotton at hsl.gov.uk wrote:
>> I am new to R and I would like to get the day of the year from 
>> numerous data in the following form:  %a %d/%b/%Y %H:%M:%S (for 
>> example from Tu 10/Jul/2007 21:59:13 I would like to get 191)
>> 
>> Whatever I try, I get NAs.
> 
> The problem is th 'Tu' bit.  Abbreviated day-of-week names and three 
> letters long.  You'll have to replace/ delete them.
> 
> datestr <- "Tu 10/Jul/2007 21:59:13"
> newdatestr <- sub("Tu", "Tue", datestr)
> dateval <- strptime(newdatestr, "%a %d/%b/%Y %H:%M:%S")
> dateval$yday            #190

In addition to the above: BEWARE that strptime(...)$yday counts
upwards from 0 to 364 (normal year) or 365 (leap year). In other
words, 1 Jan is day 0.

Hence Richard got the result 190 where you expected 191.
So add 1 to get the answer everyone expects!

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-Oct-08                                       Time: 12:24:12
------------------------------ XFMail ------------------------------



More information about the R-help mailing list