Re: [R] How to get week of the year, scale 1-52?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat, 22 Mar 2008 15:57:08 +0100

Eik Vettorazzi wrote:
> But usually weeks start with a Monday. So what should happen when new
> year isn't on a Monday? format(days, "%W") puts the first Monday of the
> year in the first week, which makes sense.
>
>
No it doesn't. Well, it does, but it will only match our planning calendars in some years. For ISO dates, the rule is that the week crossing New Year belongs to the year containing the majority of its days. Consider the following:

 > x <- as.Date(paste(1999:2009, "-12-31", sep=""))  > cbind(date=format(x), ISOdate=format(x, format="%G: Week %V, day %u"))

      date ISOdate

 [1,] "1999-12-31" "1999: Week 52, day 5"
 [2,] "2000-12-31" "2000: Week 52, day 7"
 [3,] "2001-12-31" "2002: Week 01, day 1"
 [4,] "2002-12-31" "2003: Week 01, day 2"
 [5,] "2003-12-31" "2004: Week 01, day 3"
 [6,] "2004-12-31" "2004: Week 53, day 5"
 [7,] "2005-12-31" "2005: Week 52, day 6"
 [8,] "2006-12-31" "2006: Week 52, day 7"
 [9,] "2007-12-31" "2008: Week 01, day 1"
[10,] "2008-12-31" "2009: Week 01, day 3" [11,] "2009-12-31" "2009: Week 53, day 4"

> hth.
>
> Lauri Nikkinen schrieb:
>
>> Yeah, that's correct. I can accept that there is more than 52 weeks in
>> a year (:-)) but it's hard to accept that the first week e.g in this
>> example is 00 (year 2008). Should the first week be the first one, 1?
>>
>> -Lauri
>>
>> 2008/3/18, John Kane <jrkrideau_at_yahoo.ca>:
>>
>>
>>> 365/7 != 52. You have more than 52 weeks in each
>>> year.
>>>
>>>
>>> --- Lauri Nikkinen <lauri.nikkinen_at_iki.fi> wrote:
>>>
>>>
>>>
>>>> R users,
>>>>
>>>> I have a vector of dates
>>>>
>>>> days <- seq(as.Date("2007/1/1"),
>>>> as.Date("2008/1/31"), "days")
>>>>
>>>> and I would like to have week numbers from 1 to 52
>>>> for each year. How
>>>> do I do that? Now I get 00-53 using
>>>>
>>>> format(days, "%W")
>>>>
>>>>
>>>>
>>>>> range(format(days, "%W"))
>>>>>
>>>>>
>>>> [1] "00" "53"
>>>>
>>>> I have read "Date and Time Classes in R" (R news
>>>> 01/04 by Gabor
>>>> Grothendieck and Thomas Petzoldt) with no help.
>>>>
>>>> Thanks
>>>> Lauri
>>>>
>>>> ______________________________________________
>>>> R-help_at_r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained,
>>>> reproducible code.
>>>>
>>>>
>>>>
>>> Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail. Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca
>>>
>>>
>>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat 22 Mar 2008 - 15:02:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 22 Mar 2008 - 15:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive