How can I assign a level based on if a date is within a range?

Viewed 97

I need to create a column in a dateframe that says what date range each observation is within.

Sample data:

df <- data.frame(dt = as.POSIXct(c("2018-01-01 08:00:00", "2018-03-05 12:03:00", "2018-08-16 03:07:00")), dur = c(0, 24, 12), doo = c(3.22, 6.63, 7.13))

to get

| dt                  | dur | doo  |
|---------------------|:---:|------|
| 2018-01-01 08:00:00 | 0   | 3.22 |
| 2018-03-05 12:03:00 | 24  | 6.63 |
| 2018-08-16 03:07:00 | 12  | 7.13 |

I've assigned intervals for the "months" i need to use with lubridate:

mo01 <- interval(ymd("2018-01-01"), ymd("2018-01-28"))
mo03 <- interval(ymd("2018-02-26"), ymd("2018-04-01"))
mo08 <- interval(ymd("2018-07-30"), ymd("2018-08-26"))

What I'd like to get to is a table that looks like this

| dt                  | dur | doo  | mo |
|---------------------|:---:|------|----|
| 2018-01-01 08:00:00 | 0   | 3.22 |mo01|
| 2018-03-05 12:03:00 | 24  | 6.63 |mo03|
| 2018-08-16 03:07:00 | 12  | 7.13 |mo08|

When I've run something like this (thinking I'd have to run for each month)

for (a in df$dt){
  if (a %within% mo01){
    df$mo = "January"
    }
  }

I get

Error in a %within% mo01 : Argument 1 is not a recognized date-time 

df$dt is class "POSIXct" "POSIXt"

I'd really like to be able to run one chunk of code that looks at all of the dates in dt and populates the mo column based on what date range the observation is in.

0 Answers
Related