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

Reinstate support for filtering and arranging lubridate Periods and Intervals #3695

Closed
richierocks opened this issue Jun 24, 2018 · 7 comments

Comments

@richierocks
Copy link

Support for filter()ing and arrange()ing columns of class Period and Interval was recently dropped from dplyr. The ability to work with these types is useful, so I'd like the feature reinstating.

(By 'useful', I mean in particular that there are DataCamp courses depending upon this feature. For example, Working with Dates and Times in R.)


To reproduce:

library(dplyr)                        
library(lubridate)                    
x <- data_frame(                      
  start = as.Date("2000-01-01") + 0:19, 
  finish = as.Date("2001-01-01") + 19:0,
  interval = start %--% finish          
)                                     
x %>%                                 
  arrange(interval)                     
#> Error in arrange_impl(.data, dots): Column `interval` classes Period and Interval from lubridate are currently not supported.
                                      
x %>%                                 
  filter(interval > 0)                  
#> Error in filter_impl(.data, quo): Column `interval` classes Period and Interval from lubridate are currently not supported.
@krlmlr
Copy link
Member

krlmlr commented Jun 25, 2018

Are you sure these features worked as expected? How is comparison defined between intervals?

We should definitely introduce proper support for these and other data types, #2432 is the canonical issue.

@richierocks
Copy link
Author

@krlmlr Comparison for intervals was on the length of the interval (in seconds) – I think this is reasonable. You can use unclass(lubridate::int_length(x)).

@romainfrancois
Copy link
Member

There are a few things we need to figure out before we can make these things work again. Some code will need to be factored out of dplyr or reimplemented so that 📦 like lubridate can implement a method that does the right thing without necessarily depend on dplyr.

This is probably for the 0.9.0 horizon at least

@hadley
Copy link
Member

hadley commented Oct 1, 2018

filter() should work in 0.8.0, arrange() will need to wait for 0.9 & vctrs support

@romainfrancois
Copy link
Member

They both work for intervals, but the comparison is only done through the @.Data slot I guess.

library(dplyr, warn.conflicts = FALSE)                        
library(lubridate, warn.conflicts = FALSE)                    
x <- data_frame(                      
  start = as.Date("2000-01-01") + 0:3, 
  finish = as.Date("2001-01-01") + 3:0,
  interval = start %--% finish          
) 
x$interval
#> [1] 2000-01-01 UTC--2001-01-04 UTC 2000-01-02 UTC--2001-01-03 UTC
#> [3] 2000-01-03 UTC--2001-01-02 UTC 2000-01-04 UTC--2001-01-01 UTC
str(x$interval)
#> Formal class 'Interval' [package "lubridate"] with 3 slots
#>   ..@ .Data: num [1:4] 31881600 31708800 31536000 31363200
#>   ..@ start: POSIXct[1:4], format: "2000-01-01" ...
#>   ..@ tzone: chr "UTC"

x %>%                                 
  arrange(interval)                     
#> # A tibble: 4 x 3
#>   start      finish     interval                      
#>   <date>     <date>     <S4: Interval>                
#> 1 2000-01-04 2001-01-01 2000-01-04 UTC--2001-01-01 UTC
#> 2 2000-01-03 2001-01-02 2000-01-03 UTC--2001-01-02 UTC
#> 3 2000-01-02 2001-01-03 2000-01-02 UTC--2001-01-03 UTC
#> 4 2000-01-01 2001-01-04 2000-01-01 UTC--2001-01-04 UTC

x %>%                                 
  arrange(interval@.Data)                     
#> # A tibble: 4 x 3
#>   start      finish     interval                      
#>   <date>     <date>     <S4: Interval>                
#> 1 2000-01-04 2001-01-01 2000-01-04 UTC--2001-01-01 UTC
#> 2 2000-01-03 2001-01-02 2000-01-03 UTC--2001-01-02 UTC
#> 3 2000-01-02 2001-01-03 2000-01-02 UTC--2001-01-03 UTC
#> 4 2000-01-01 2001-01-04 2000-01-01 UTC--2001-01-04 UTC

x %>%                                 
  filter(interval > 0)  
#> # A tibble: 4 x 3
#>   start      finish     interval                      
#>   <date>     <date>     <S4: Interval>                
#> 1 2000-01-01 2001-01-04 2000-01-01 UTC--2001-01-04 UTC
#> 2 2000-01-02 2001-01-03 2000-01-02 UTC--2001-01-03 UTC
#> 3 2000-01-03 2001-01-02 2000-01-03 UTC--2001-01-02 UTC
#> 4 2000-01-04 2001-01-01 2000-01-04 UTC--2001-01-01 UTC

x %>%                                 
  filter(interval@.Data > 0)  
#> # A tibble: 4 x 3
#>   start      finish     interval                      
#>   <date>     <date>     <S4: Interval>                
#> 1 2000-01-01 2001-01-04 2000-01-01 UTC--2001-01-04 UTC
#> 2 2000-01-02 2001-01-03 2000-01-02 UTC--2001-01-03 UTC
#> 3 2000-01-03 2001-01-02 2000-01-03 UTC--2001-01-02 UTC
#> 4 2000-01-04 2001-01-01 2000-01-04 UTC--2001-01-01 UTC

Created on 2018-12-14 by the reprex package (v0.2.1.9000)

@hadley
Copy link
Member

hadley commented May 27, 2019

Duplicate of #2432

@hadley hadley marked this as a duplicate of #2432 May 27, 2019
@hadley hadley closed this as completed May 27, 2019
@lock
Copy link

lock bot commented Nov 23, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Nov 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants