tidyverse / dbplyr

Database (DBI) backend for dplyr
https://dbplyr.tidyverse.org
Other
474 stars 173 forks source link

Adding translations for clock::date_count_between() #1495

Open edward-burn opened 5 months ago

edward-burn commented 5 months ago

To go with existing clock translations (like these https://github.com/tidyverse/dbplyr/blob/8f2fcb0852994afe11cc6792ef59ef5ffda84936/R/backend-postgres.R#L240), one other function from clock that I think would be very useful to translate would be date_count_between, so code like below would work if the data were in a database.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(clock)

test_data <- data.frame(
  person = 1L,
  date_1 = as.Date("1980-01-01"),
  date_2 = as.Date("2010-01-01")
) 

test_data |> 
  mutate(days = date_count_between(date_1, date_2, "day"))
#>   person     date_1     date_2  days
#> 1      1 1980-01-01 2010-01-01 10958

Created on 2024-05-04 with reprex v2.1.0