-1

I am using DBPLYR to access a database. However, I would like to filter on two sets of dates. I know how to do this in SQL but not in dplyr or dbplyr.

The SQL code for this would be

(start_date between date '2017-01-01' and date '2017-03-31') or (start_date 
between date '2018-01-01' and date '2018-03-31'))

How would I convert this to dplyr syntax?

  • 2
    Check out [`dplyr::between`](https://stackoverflow.com/questions/39997225/how-does-dplyr-s-between-work) – CPak Jun 18 '18 at 13:58

1 Answers1

1
x <- data.frame(date = sample(seq(as.Date('2017/01/01'), as.Date('2018/12/31'), by="day"), 365))

library(dplyr)

x %>%
  filter(date > "2017-01-01" & date < "2017-03-01" |
           date > "2018-01-01" & date < "2018-03-31")
Steven
  • 3,238
  • 21
  • 50