10

Is there a function in lubridate to extract the week number? I've tried to search for that but couldn't find anything which serves the purpose.

The week() function does something different.

Description

Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, > its, ti, jul, timeSeries, and fts objects. Weeks is the number of complete seven day periods that have occured between the date and January 1st, plus one. isoweek returns the week as it would appear in the ISO 8601 system, which uses a reoccuring leap week.

Community
  • 1
  • 1
Gianluca
  • 6,307
  • 19
  • 44
  • 65
  • 1
    Check `?strptime` (in base R) and then check `%W` or `%U` – David Arenburg Sep 16 '14 at 10:34
  • 1
    What's the week number for you ? For example if the january 1st is Thursday, which week number do you expect on january 6th (Monday) ? Also, what's your input ? A datetime in string format ? – digEmAll Sep 16 '14 at 10:51
  • http://cran.r-project.org/web/packages/ISOweek/ISOweek.pdf – Roland Sep 16 '14 at 10:52
  • 2
    In the help text you cite, the `lubridate` function `isoweek` is mentioned. If neither `week` or `isoweek` doesn't provide the answer, you need to clarify [**which week system**](http://en.wikipedia.org/wiki/Seven-day_week#Week_numbering) you are looking for. – Henrik Sep 16 '14 at 11:30

1 Answers1

12

Use strftime:

dateRange <- c("2008-10-01","2008-12-01") 
x <- as.POSIXlt(dateRange) 
strftime(x,format="%W") 
[1] "39" "48" 
Prasanna Nandakumar
  • 4,295
  • 34
  • 63