[R] Week value function

0 views
Skip to first unread message

Pele

unread,
Mar 7, 2009, 12:34:35 PM3/7/09
to r-h...@r-project.org

Hi R users,

I am looking for a date function that will give the following:
- The number-of-week value is in the range 01-53
- Weeks begin on a Monday and week 1 of the year is the week that
includes both January 4th and the first Thursday of the year.
If the first Monday of January is the 2nd, 3rd, or 4th, the
preceding days are part
of the last week of the preceding year. This is similar to the
SAS's week function with option V.

I am currently using :

date <- strptime(DATE, "%d%B%Y")
week <- format(date, "%W")

but, I could not find an option for doing the above description
automatically. Can anyone help?

Thanks in advance for any help.

--
View this message in context: http://www.nabble.com/Week-value-function-tp22389878p22389878.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@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.

David Winsemius

unread,
Mar 7, 2009, 1:20:17 PM3/7/09
to Pele, r-h...@r-project.org
I am not seeing anything but that proves nothing of course. You could
write your own function and stick it in the .First of your .Rprofile
files that get loads at startup.

Details here:
http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_first.html

week.dBY <- function(x) format(strptime(x, "%d%B%Y") , "%W")

> dt<-"07JAN2009"
> week.dBY(dt)
[1] "01" # a character valued vector

Gives "00" for "01JAN2009" but you can adjust that behavior to your
specifications. You could also convert to numeric if desired:

> nweek.dBY <- function(x) as.integer(format(strptime(x, "%d%B%Y") ,
"%W"))
> nweek.dBY(dt)
[1] 1

--
David Winsemius

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

Pele

unread,
Mar 7, 2009, 9:30:49 PM3/7/09
to r-h...@r-project.org

Hi David - I will try that..

Thanks for your suggestion!

--
View this message in context: http://www.nabble.com/Week-value-function-tp22389878p22394294.html

Reply all
Reply to author
Forward
0 new messages