[R] question about date's

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Dec 12 15:44:59 CET 2005


On Mon, 12 Dec 2005, Peter Dalgaard wrote:

> Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:
>
>> On Mon, 12 Dec 2005, ronggui wrote:
>>
>>>> x<-as.Date(c("2005-07-01", "2005-07-02","2005-07-03","2005-07-04","2005-07-05"))
>>>> weekdays(x)
>>> [1] "ÐÇÆÚÎå" "ÐÇÆÚÁù" "ÐÇÆÚÈÕ" "ÐÇÆÚÒ»" "ÐÇÆÚ¶þ"
>>>
>>>> months(x)
>>> [1] "ÆßÔÂ" "ÆßÔÂ" "ÆßÔÂ" "ÆßÔÂ" "ÆßÔÂ"
>>
>>
>> He asked for week numbers.  That's nothing like as easy, as it is not
>> well-defined.  But
>>
>>> strftime(as.POSIXlt(x), "%U")
>> [1] "26" "26" "27" "27" "27"
>>
>> is one possibility ("%W" is another).  This approach will do the other
>> requests just as easily.
>
> %W seems to be what is known as "ISO dates" (week starts on Monday),
> except that
>
>>  strftime(as.POSIXlt(as.Date("2005-01-01")), "%U")
> [1] "00"
>
> should be week 53, 2004 according to my printed calendar, and emacs
> calendar-mode too.

I _did_ say

>> That's nothing like as easy, as it is not well-defined.

The POSIX definition is

%U
     Replaced by the week number of the year as a decimal number [00,53].
     The first Sunday of January is the first day of week 1; days in the
     new year before this are in week 0.

%W
     Replaced by the week number of the year as a decimal number [00,53].
     The first Monday of January is the first day of week 1; days in the
     new year before this are in week 0.

so it is doing what it is documented to do.  I'd take POSIX as more 
definitive than Emacs ....

-- 
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