Generate which 15-days period a day fall into

2019-08-02 03:23发布

问题:

I have a data frame with year and day

df <- data.frame(year = rep(1980:2015,each = 365), day = 1:365)

Please note that I only need 365 days a year i.e. I am asusming each day has 365 years.

I want to generate two data: 1) which month does each day fall in 2) which 15-days period each day fall in. A year will have 24 15-days period. i.e. each month will be split into two halves something like this;

     Jan: 1st - 15th: 1st Quarter
     Jan: 16th- 31st: 2nd Quarter
     Feb: 1st - 15th: 3rd Quarter
     Feb: 16th - 28th: 4th Quarter
     March: 1st - 15th: 5th Quarter
     .
     .
     Decmber: 16th - 31st: 24th quarter

My final data should look like this

       Year Day  Quarter   Month
       1980  1    1          1
       1980  2    1          1
        .
        .
       1980  365   24        12
        . 
        .
       2015  1    1          1
       2015  2    1          1
        .
        .
       2015  365   12        24

I can generate the month using this:

   library(dplyr)
   months <- list(1:31, 32:59, 60:90, 91:120, 121:151, 152:181, 182:212, 213:243, 244:273, 274:304, 305:334, 335:365)

    df1 <- df %>% group_by(year) %>% 
          mutate(month = sapply(day, function(x) which(sapply(months, function(y) x %in% y)))

But I do not know how to generate the 15-days period?

回答1:

To handle that Feb 29th in leap years should not be included, we may generate a complete sequence of dates and then remove instances of Feb 29th. Grab month from the date. Calculate the two-week periods by checking if day of the month %d is <= 15 and subtract from 2* the month number.

# complete sequence of dates
# use two years in this example, with 2012 being a leap year
dates <- data.frame(date = seq(as.Date("2011-01-01"), as.Date("2012-12-31"), by = "1 day"))

# remove Feb 29th in leap years
d <- dates[format(dates$date, "%m-%d") != "02-29", , drop = FALSE]

# create month
d$month <- month(d$date)

# create two-week number
d$twoweek <- d$month * 2 - (as.numeric(format(d$date, "%d")) <= 15)


标签: r dplyr as.date