Mark data between two dates

I need to add a column that marks "1" if the data falls in a certain range.

structure(list(Start = structure(c(1583107200, 1580515200, 1603152000, 
1590969600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    End = structure(c(1589932800, 1585526400, 1605830400, 1593561600
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

I need to code where if the row has the Start and End dates between April 1, 2020 to October 31, 2020 it adds a Column called Active and puts a "1" for it and "0" otherwise. For example one of my rows has a Start of March 2, 2020 and for the End column it has May 20, 2020. So because the Start and End has days active between April 1, 2020 to October 31, 2020 the new column named Active should be set to 1 for that row. Hope someone can help!

Something like this perhaps?

library(lubridate)
library(tidyverse)

interval_dates <- interval(ymd("2020-04-01"), ymd("2020-10-31"))

df %>% 
  mutate(start_within = Start %within% interval_dates,
         end_within = End %within% interval_dates,
         period_within = if_else(start_within == TRUE & end_within == TRUE, 1, 0))

# A tibble: 4 × 5
  Start               End                 start_within end_within period_within
  <dttm>              <dttm>              <lgl>        <lgl>              <dbl>
1 2020-03-02 00:00:00 2020-05-20 00:00:00 FALSE        TRUE                   0
2 2020-02-01 00:00:00 2020-03-30 00:00:00 FALSE        FALSE                  0
3 2020-10-20 00:00:00 2020-11-20 00:00:00 TRUE         FALSE                  0
4 2020-06-01 00:00:00 2020-07-01 00:00:00 TRUE         TRUE                   1
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.