[R] How to assign week numbers to a time-series
Achim Zeileis
Achim.Zeileis at uibk.ac.at
Sat Mar 6 00:21:45 CET 2010
On Fri, 5 Mar 2010, Hosack, Michael wrote:
> Hello everyone,
>
> My progress has stalled on finding a way of creating a somewhat complicated variable to add to my existing dataframe and I am hoping one of you could help me out. The dataframe below contains only a fraction of the data of my complete dataframe, but all of the variables. What I want to do is add another variable named 'WEEK' to this dataframe that is assigned 1 for row 1 and remains 1 until the first SAT (i.e. Saturday) under variable 'DOW' (day of week) occurs, at which point variable 'WEEK' is now assigned 2. 'WEEK' should continue to be assigned 2 until the following SAT under variable 'DOW' at which variable 'WEEK' will now be assigned 3, and so on. In this scheme, weekdays are such that SAT=1, SUN=2, MON=3,.....FRI=7. I am basically trying to assign week numbers to potential sampling days in a survey season for use in a program that will generate a fisheries creel survey schedule. I should note that if element 1 happens to have DOW=SAT (that is the case this year, sin!
ce!
> the first day of our survey 05/01 is a Saturday), then WEEK 1 begins on day 1 (05/01/2010) and WEEK 2 will begin on the first SAT under variable DOW. I hope I explained this clearly enough, if not let me know.
If I understand correctly, then you can easily do the following:
- create a "Date" object with YR-MM-DD
- compute the number of days since origin (2010-04-30 in your case)
- compute integer division by 7 plus 1
In one step:
as.numeric(as.Date(paste(YR, MM, DD, sep = "-")) -
as.Date("2010-04-30")) %/% 7 + 1
BTW: Some of the rest of your information (like DOW) could also be easily
inferred from the "Date", e.g., via transformation to POSIXlt:
as.POSIXlt(as.Date("2010-05-01"))$wday
hth,
Z
> Mike
>
> MM DD YR DOW DOW. DTYPE TOD TOD. SITENUM DESC
> 1 05 01 2010 SAT 1 2 MORN 1 101 WALNUT.CK
> 185 05 01 2010 SAT 1 2 MORN 1 102 LAMPE
> 369 05 01 2010 SAT 1 2 MORN 1 103 EAST.AVE
> 553 05 01 2010 SAT 1 2 MORN 1 104 NORTH.EAST
> 737 05 01 2010 SAT 1 2 AFTN 2 101 WALNUT.CK
> 921 05 01 2010 SAT 1 2 AFTN 2 102 LAMPE
> 1105 05 01 2010 SAT 1 2 AFTN 2 103 EAST.AVE
> 1289 05 01 2010 SAT 1 2 AFTN 2 104 NORTH.EAST
> 2 05 02 2010 SUN 2 2 MORN 1 101 WALNUT.CK
> 186 05 02 2010 SUN 2 2 MORN 1 102 LAMPE
> 370 05 02 2010 SUN 2 2 MORN 1 103 EAST.AVE
> 554 05 02 2010 SUN 2 2 MORN 1 104 NORTH.EAST
> 738 05 02 2010 SUN 2 2 AFTN 2 101 WALNUT.CK
> 922 05 02 2010 SUN 2 2 AFTN 2 102 LAMPE
> 1106 05 02 2010 SUN 2 2 AFTN 2 103 EAST.AVE
> 1290 05 02 2010 SUN 2 2 AFTN 2 104 NORTH.EAST
> 3 05 03 2010 MON 3 1 MORN 1 101 WALNUT.CK
> 187 05 03 2010 MON 3 1 MORN 1 102 LAMPE
> 371 05 03 2010 MON 3 1 MORN 1 103 EAST.AVE
> 555 05 03 2010 MON 3 1 MORN 1 104 NORTH.EAST
> 739 05 03 2010 MON 3 1 AFTN 2 101 WALNUT.CK
> 923 05 03 2010 MON 3 1 AFTN 2 102 LAMPE
> 1107 05 03 2010 MON 3 1 AFTN 2 103 EAST.AVE
> 1291 05 03 2010 MON 3 1 AFTN 2 104 NORTH.EAST
> 4 05 04 2010 TUE 4 1 MORN 1 101 WALNUT.CK
> 188 05 04 2010 TUE 4 1 MORN 1 102 LAMPE
> 372 05 04 2010 TUE 4 1 MORN 1 103 EAST.AVE
> . . . . . . . . . . .
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list