Lubridate week() to find consecutive week number for multi-year periods

tumultous_rooster picture tumultous_rooster · Nov 5, 2013 · Viewed 16.4k times · Source

Within R, say I have a vector of some Lubridate dates:

> Date
"2012-01-01 UTC"
"2013-01-01 UTC"

Next, suppose I want to see what week number these days fall in:

> week(Date)
1
1

Lubridate is fantastic!

But wait...I'm dealing a time series with 10,000 rows of data...and the data spans 3 years.

I've been struggling with finding some way to make this happen:

> result of awesome R code here
1
54

The question: is there a succinct way to coax out a list of week numbers over multiyear periods within Lubridate? More directly, I would like the first week of the second year to be represented as the 54th week. And the first week in the third year to be represented as the 107th week, ad nauseum.

So far, I've attempted a number of hackney schemes but cannot seem to create something not fastened together with scotch tape. Any advice would be greatly appreciated. Thanks in advance.

Answer

beroe picture beroe · Nov 5, 2013

To get the interval from a particular date to another date, you can just subtract...

If tda is your vector of dates, then

tda - min(tda)

will be the difference in seconds between them.

To get the units out in weeks:

(tda - min(tda))/eweeks(1)

To do it from a particular date:

tda - ymd(19960101)

This gives the number of days from 1996 to each value.

From there, you can divide by days per week, or seconds per week.

(tda - ymd(19960101))/eweeks(1)

To get only the integer part, and starting from January 2012:

trunc((tda - ymd(20111225))/eweeks(1))

Test data:

tda = ymd(c(20120101, 20120106, 20130101, 20130108))

Output:

 1  1 53 54