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

Liaw, Andy andy_liaw at merck.com
Tue Mar 18 14:01:29 CET 2008


Looks like you can convert to Julian date with the right origin and then
divide by 7, and take modulo 52.  Something like:

 julian(days, origin=days[1]-1) %/% 7 %% 52

That gets you 0-51.  You just need to add one to get what you want.  (I
subtract one from days[1] because it's a Monday.  I'm sure you can
figure out how to do that programatically.)

Andy 

From: Lauri Nikkinen
> 
> 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.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachme...{{dropped:15}}



More information about the R-help mailing list