Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

lubridate

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.

like image 334
tumultous_rooster Avatar asked Nov 05 '13 01:11

tumultous_rooster


2 Answers

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
like image 196
beroe Avatar answered Sep 20 '22 07:09

beroe


Since eweeks() is now deprecated, I thought I'd add to @beroe's answer.

If tda is your date vector, you can get the week numbers with:

weeknos <- (interval(min(tda), tda) %/% weeks(1)) + 1

where %/% causes integer division. ( 5 / 3 = 1.667; 5 %/% 3 = 1)

like image 34
s-heins Avatar answered Sep 18 '22 07:09

s-heins