If you don't want to rely on the week calculations in R (which in
turn may depend on the OS?) you could try doing it yourself. The
formula is pretty simple.
Suppose:
fw = day of week of first day of each week (0=Sun, 1=Mon, etc.)
fy = day of week of first day of year (to be calculated given year)
w = week number
Then (this is math, not R):
mod(fw-fy,7)+7(w-1)
is the day of the year number of the first day of the wth week
where 0 represents the first day of the year. Just add this
number of days to the date of the first of the year.
This can be translated into R using chron like this. The first
two lines of the function body extract out the year and week.
The third gets the date of the first of the year. Then
we calculate the date of the first of the year, foy. The fourth
line calculates the day of the first of the year, fy. The
5th line uses the formula above to get the day number and the
last line adds it to the date of the first of the year.
yw.to.date <- function( x, fw = 0 ) {
y <- as.numeric( substring( x, 1, 4 ) )
w <- as.numeric( substring( x, 5 ) )
foy <- chron( paste( 1, 1, y, sep= "/" ) )
fy <- day.of.week( 1, 1, year )
dayno <- ( fw - fy ) %% 7 + 7 * ( w - 1 )
foy + dayno
}
# test
require( chron )
yw.to.date( c( "200302", "200310" ) )
or using POSIXt like this:
yw.to.date <- function( x, fw = 0 ) {
y <- as.numeric( substring( x, 1, 4 ) )
w <- as.numeric( substring( x, 5 ) )
foy <- ISOdate( year, 1, 1, tz = "" )
fy <- as.POSIXlt( foy )$wday
dayno <- ( fw - fy ) %% 7 + 7 * ( w - 1 )
foy + dayno * 24 * 60 * 60
}
# test
yw.to.date( c( "200302", "200310" ) )
---
Date: 15 Dec 2003 13:35:37 +0100
From: Peter Dalgaard <p.dalgaard at biostat.ku.dk>
To: Berwin Turlach <berwin at maths.uwa.edu.au>
Cc: <R-help at stat.math.ethz.ch>,Barry Rowlingson <B.Rowlingson at
lancaster.ac.uk>,Wayne Jones <JonesW at kssg.com>
Subject: Re: [R] Week of the Year date conversion
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)