Home > Software engineering >  How can I check if a date falls between two other dates?
How can I check if a date falls between two other dates?

Time:11-08

I have a dataframe with 7 columns of date variables: start.date, end.date, date1, date2, date3, date4, date5. The dates are in the format yyyy-mm-dd.

I want to create a new variable that tells me how many of date1, date2, date3, date4, date5 fall between start.date and end.date using R, is this possible?

CodePudding user response:

Hi you could use %within% from lubridate:

Data I use:

library(dplyr)
library(lubridate)
df <- nycflights13::flights |> 
  mutate(start = as_date(time_hour),
         end = start   days(5)) |>
  slice(1:2) |> 
  select(start, end) |> 
  mutate(test_date = c("2013-01-10", "2013-01-03") |> ymd())
# A tibble: 2 × 3
  start      end        test_date 
  <date>     <date>     <date>    
1 2013-01-01 2013-01-06 2013-01-10
2 2013-01-01 2013-01-06 2013-01-03

Testing:

df |> 
  mutate(log_test = test_date %within% interval(start, end))

Output is:

# A tibble: 2 × 4
  start      end        test_date  log_test
  <date>     <date>     <date>     <lgl>   
1 2013-01-01 2013-01-06 2013-01-10 FALSE   
2 2013-01-01 2013-01-06 2013-01-03 TRUE    
  • Related