[R] format() method

David Winsemius dwinsemius at comcast.net
Fri Apr 16 17:16:39 CEST 2010


On Apr 16, 2010, at 9:28 AM, kafkaz wrote:

>
> Hello,
> I use format() function to get number of the week, like this:
> format(tmp,'%U')
> Recently, I have spotted something bizarre. For example, I have such  
> object:
> (index(tmp$x.delta['2009'][1:16]))
> [1] "2009-01-02 CET"  "2009-01-09 CET"  "2009-01-16 CET"   
> "2009-01-23 CET"
> [5] "2009-01-30 CET"  "2009-02-06 CET"  "2009-02-13 CET"   
> "2009-02-20 CET"
> [9] "2009-02-27 CET"  "2009-03-06 CET"  "2009-03-13 CET"   
> "2009-03-20 CET"
> [13] "2009-03-27 CET"  "2009-04-03 CEST" "2009-04-09 CEST"  
> "2009-04-17 CEST"
> dput(index(tmp$x.delta['2009'][1:16]),'%U',file='as.date')
> structure(c(1230850800, 1231455600, 1232060400, 1232665200,  
> 1233270000,
> 1233874800, 1234479600, 1235084400, 1235689200, 1236294000,  
> 1236898800,
> 1237503600, 1238108400, 1238709600, 1239228000, 1239919200), tzone =
> structure("", .Names = "TZ"), class = c("POSIXt",
> "POSIXct"))
> To get number of the week I run:
> format(index(tmp$x.delta['2009'][1:16]),'%U')
> Here is the output - the weird thing is, that the first number of  
> the week
> is 00.

Appears to behave as documented. From ?formatPOSIXct (help page):

"%U
Week of the year as decimal number (00–53) using Sunday as the first  
day 1 of the week..."


> [1] "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12"  
> "13"
> "14"
> [16] "15"
>
> Is it the bug, my mistake or it is supposed to by like that?
> Thank you,
> kafka

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list