3

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.

ajbentley
  • 193
  • 1
  • 10
  • 1
    So `mo` column is just the month of `dt` ? You can try `paste0("mo", format(df$dt, "%M"))` or with `lubridate` `paste0("mo", month(df$dt))` – Ronak Shah Jan 15 '19 at 23:58
  • It's not a standard month so %M won't work – ajbentley Jan 16 '19 at 00:41
  • Relevant previous question - https://stackoverflow.com/questions/41132081/find-which-interval-row-in-a-data-frame-that-each-element-of-a-vector-belongs-in/41133991 - since dates/times are just essentially numeric values, most of the answers there should work. – thelatemail Jan 16 '19 at 01:12

0 Answers0