Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

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)

You can do something like this :

week(dat) +53*(year(dat)-min(year(dat)))

Given you like lubridate (as do I)

year_week <- function(x,base) week(x) - week(base) + 52*(year(x) - year(base))
test <- ymd(c(20120101, 20120106, 20130101, 20130108))
year_week(test, "2012-01-01")

Giving

[1]  0  0 52 53
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top