[R] Week of the Year date conversion

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Dec 15 13:35:37 CET 2003


Berwin Turlach <berwin at maths.uwa.edu.au> writes:

> >>>>> "BR" == Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes:
> 
>     BR> Ah ha. Use '%w' for 'day of week':
> 
>     >> strptime("2003 05 06", format="%Y %U %w")
>     BR> [1] "2003-02-02"
> 
>     BR> I dont have a calendar to hand to check that 2 Feb is the 6th
>     BR> day of the 5th week....
> It's not, it is the first day of the 5th week in 2003.  Moreover:
> 
> > strptime(paste("2003 05 0", 0:6, sep=""), format="%Y %U %w")
> [1] "2003-02-02" "2003-02-02" "2003-02-02" "2003-02-02" "2003-02-02"
> [6] "2003-02-02" "2003-02-02"
> 
> I guess this behaviour is consistent with the description of %U:
>  
>     `%U' Week of the year as decimal number (00-53) using the first
>           Sunday as day 1 of week 1.
> 
> ??
> 
> > strptime(paste("2003 01 0", 0:6, sep=""), format="%Y %U %w")
> [1] "2003-01-05" "2003-01-05" "2003-01-05" "2003-01-05" "2003-01-05"
> [6] "2003-01-05" "2003-01-05"

 
Why on earth are you guys trying to use a two-digit field for a
quantity in the range 0:6 ??

 > strptime(paste("2003 01 ", 0:6, sep=""), format="%Y %U %w")
[1] "2003-01-05" "2003-01-06" "2003-01-07" "2003-01-08" "2003-01-09"
[6] "2003-01-10" "2003-01-11"


However, this is ISO week *2*!  Day 1 of Week 1, 2003 was Dec 30,
2002.

Now, arguably we are counting from zero, but watch this:

> strptime(paste("2003 00", 0:6), format="%Y %U %w")
[1] "2002-11-18" "2002-11-19" "2002-11-20" "2003-01-01" "2003-01-02"
[6] "2003-01-03" "2003-01-04"

and further,

> strptime(paste("2003 01", 0:6), format="%Y %U %w")
[1] "2003-01-05" "2003-01-06" "2003-01-07" "2003-01-08" "2003-01-09"
[6] "2003-01-10" "2003-01-11"
> strptime(paste("2003 01", 0:6), format="%Y %W %w")
[1] "2003-01-05" "2003-01-06" "2003-01-07" "2003-01-08" "2003-01-09"
[6] "2003-01-10" "2003-01-11"

> 5 January 2003 was the first Sunday in 2003.


Yeah, but what's the difference between %U and %W then? 

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

     '%w' Weekday as decimal number (0-6, Sunday is 0).

     '%W' Week of the year as decimal number (00-53) using the first
          Monday as day 1 of week 1.

Neither of those definitions coincide with ISO, BTW.

(This probably all comes down to OS/libc deficiencies. RedHat 8 in this case)




More information about the R-help mailing list