[R] Assigning Week Numbers

David Winsemius dwinsemius at comcast.net
Wed Apr 21 17:08:20 CEST 2010


On Apr 21, 2010, at 9:40 AM, Michael Hosack wrote:

> Thank you David, this approach is a start in the right direction but  
> it does
> not yield the needed results. I need new week numbers to only begin
> on Saturdays. The only exception will be for the first date (May 01)
> which will start week 1 on a different day of the week depending on
> the year. The proceeding Saturday will begin Week #2. The approach you
> provided does not end Week 1 on a Friday and does not end subsequent
> week #'s on Fridays.

My solution should be adjustable for any day-of-week ending number.  
Why don't you provide a minimal example for testing and show how apply  
my solution fails? Your original version was definitely not "minimal."

-- 
David.
>
> Thanks again,
>
> Mike
>
> ----------------------------------------
>> CC: mhosack9 at hotmail.com; r-help at r-project.org
>> From: dwinsemius at comcast.net
>> To: dwinsemius at comcast.net
>> Subject: Re: [R] Assigning Week Numbers
>> Date: Tue, 20 Apr 2010 16:03:09 -0400
>>
>>
>> On Apr 20, 2010, at 2:55 PM, David Winsemius wrote:
>>
>>>
>>> On Apr 20, 2010, at 1:59 PM, Michael Hosack wrote:
>>>
>>>>
>>>> R experts,
>>>>
>>>> How could I extract the week number from a date vector (in Date
>>>> class)
>>>> such that week numbering (week 1...2...) begins (May 01) and ends
>>>> (October 31) on the same specific dates each year? Week numbering
>>>> must conform to the following day numbering format
>>>> (Sat=1,Sun=2,Mon=3.....Fri=7).
>>>> This means that new weeks must begin on Saturdays, and end on  
>>>> Fridays
>>>> (except for the first date of May 01, which always begins week 1;
>>>> week 2
>>>> begins on the proceeding Saturday). This needs to be applicable
>>>> across years
>>>> to work effectively. I have tried using both vectorized and loop
>>>> approaches with
>>>> no success.
>>>>
>>>
>>> Modulo arithmetic will work if you first convert the difftime object
>>> to integer:
>>
>> And you can use teh chron function day.of.week to return an offset  
>> for
>> the first week
>>
>>> weekdays(as.Date("2013-05-01") )
>> [1] "Wednesday"
>>> day.of.week(2013, 5, 1)
>> [1] 5
>>> SCHEDULE3$wkoffset <- as.integer(SCHEDULE3$DATE. -
>> as.Date("2013-05-01")- day.of.week(2013, 5, 1) ) %/% 7
>>> range(SCHEDULE3$wkoffset)
>> [1] -1 25
>> # So you need a further adjustment of 2 to set the starting week
>> number to "1":
>>
>>> SCHEDULE3$wkoffset <- 2+as.integer(SCHEDULE3$DATE. -
>> as.Date("2013-05-01")- day.of.week(2013, 5, 1) ) %/% 7
>>> range(SCHEDULE3$wkoffset)
>> [1] 1 27
>>
>>>
>>>> SCHEDULE3$wkoffset <- as.integer(SCHEDULE3$DATE. -
>>> as.Date("2010-05-01") ) %/% 7
>>>> str(head(SCHEDULE3))
>>> 'data.frame': 6 obs. of 9 variables:
>>> $ DATE. :Class 'Date' int [1:6] 15826 15826 15826 15826 15826 15826
>>> $ YEAR : num 2013 2013 2013 2013 2013 ...
>>> $ MONTH : num 5 5 5 5 5 5
>>> $ DAY : num 1 1 1 1 1 1
>>> $ DOW : chr "Wed" "Wed" "Wed" "Wed" ...
>>> $ SITE : num 101 101 102 102 103 103
>>> $ TOD : Factor w/ 2 levels "MORN","AFTN": 1 2 1 2 1 2
>>> $ DOW. : num 5 5 5 5 5 5
>>> $ wkoffset: num 156 156 156 156 156 156
>>>> range(SCHEDULE3$DATE.)
>>> [1] "2013-05-01" "2013-10-31"
>>>> range(SCHEDULE3$wkoffset)
>>> [1] 156 182
>>>
>>>> I am including a bit of old Systat code that does the trick simply
>>>> and concisely.
>>>> If anyone knows an analogous method in R please let me know. My R
>>>> dataframe contains
>>>> all the variables and data in the Systat temp file.
>>>>
>>>> Use sched3.t
>>>> Save sched4.t
>>>> Hold
>>>> By mm dd
>>>> If bof then let week=1
>>>> Else if bog and DOW$="SAT" then let week = week + 1
>>>> Run
>>>>
>>>>
>>>> Thank you,
>>>>
>>>> Mike
>>>>
>>>>
>>>> SCHEDULE3 <-
>>>> structure(list(DATE. = structure(c(15826L, 15826L, 15826L, 15826L,
>
snipped the rather larger than minimal example.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list