简体   繁体   中英

Calculating week numbers WITHOUT a yearwise reset (i.e. week_id = 55 is valid and shows it is a year after) + with a specified start date

This probably seems straightforward, but I am pretty stumped.

I have a set of dates ~ August 1 of each year and need to sum sales by week number. The earliest date is 2008-12-08 (YYYY-MM-DD). I need to create a "week_id" field where week #1 begins on 2008-12-08. And the date 2011-09-03 is week 142. Note that this is different since the calculation of week number does not reset every year .

I am putting up a small example dataset here:

data <- data.frame(
  dates = c("2008-12-08", "2009-08-10", "2010-03-31", "2011-10-16", "2008-06-03", "2009-11-14" , "2010-05-05", "2011-09-03"))
data$date = as.Date(data$date)

Any help is appreciated

data$week_id = as.numeric(data$date - as.Date("2008-12-08")) %/% 7 + 1

This would take the day difference between the two dates and find the integer number of 7 days elapsed. I add one since we want the dates where zero weeks have elapsed since the start to be week 1 instead of week 0.

        dates       date week_id
1  2008-12-07 2008-12-07       0   # added for testing
2  2008-12-08 2008-12-08       1
3  2008-12-09 2008-12-09       1   # added for testing
4  2008-12-14 2008-12-14       1   # added for testing
5  2008-12-15 2008-12-15       2   # added for testing
6  2009-08-10 2009-08-10      36
7  2010-03-31 2010-03-31      69
8  2011-10-16 2011-10-16     149
9  2008-06-03 2008-06-03     -26
10 2009-11-14 2009-11-14      49
11 2010-05-05 2010-05-05      74
12 2011-09-03 2011-09-03     143

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM