Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Set Operations methods for Dates #1155

Closed
DanChaltiel opened this issue Feb 6, 2024 · 1 comment
Closed

Implement Set Operations methods for Dates #1155

DanChaltiel opened this issue Feb 6, 2024 · 1 comment

Comments

@DanChaltiel
Copy link

DanChaltiel commented Feb 6, 2024

Hi,

Using set operations like setdiff on dates removes the date class.

Here is a reprex with a very naive workaround:

library(lubridate)  
a = seq(as_date(1), as_date(10), by=1) 
b = seq(as_date(5), as_date(10), by=1) 

setdiff(a, b)  
#> [1] 1 2 3 4

setdiff.Date = function(x, y) base::setdiff(x, y) |> as_date()
setdiff(a, b)
#> [1] "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05"

Created on 2024-02-06 with reprex v2.0.2

Of course, the same problem and solution happen for other set operations like intersect and others.

I guess that since they are implemented for the Interval class, it would make sense to implement them for Date. I'm not using other lubridate classes much but maybe they have the same problem.

@DanChaltiel DanChaltiel changed the title Implement a setdiff.Date() method Implement Set Operations methods for Dates Feb 6, 2024
@DavisVaughan
Copy link
Member

Date comes from base R, not from lubridate, so I don't think it would be appropriate for us to write an S3 method in this case.

You can use vctrs::vec_set_difference() if you need a generic version that works with pretty much all S3 types.

library(lubridate)  
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
a = seq(as_date(1), as_date(10), by=1) 
b = seq(as_date(5), as_date(10), by=1) 

vctrs::vec_set_difference(a, b)  
#> [1] "1970-01-02" "1970-01-03" "1970-01-04" "1970-01-05"

Created on 2024-08-05 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants