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

Having intervals in a column breaks many dplyr verbs #635

Closed
Zedseayou opened this issue Feb 9, 2018 · 3 comments
Closed

Having intervals in a column breaks many dplyr verbs #635

Zedseayou opened this issue Feb 9, 2018 · 3 comments

Comments

@Zedseayou
Copy link

I am not sure if this is due to lubridate or dplyr, but currently having a column be of interval type in a tibble or data frame causes many dplyr operations to break with a somewhat uninformative error. I think I have everything up to date, but session info included regardless.

Setup and some example data:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

tbl <- tibble(
  x = 1:10,
  start = seq(
    from = ymd_hms("2018-01-01 12:00:00"),
    to = ymd_hms("2018-02-01 12:00:00"),
    length = 10
    ),
  end = seq(
    from = ymd_hms("2018-01-01 15:00:00"),
    to = ymd_hms("2018-02-01 15:00:00"),
    length = 10
  ),
  interval = start %--% end
)

Now we can try a variety of simple dplyr actions that I would expect to work, but fail for some reason to do with the interval column. The error message is not particularly illuminating as to where in the process things are going wrong.

tbl %>% mutate(y = 1:10)
#> Note: method with signature 'Interval#ANY' chosen for function '-',
#>  target signature 'Interval#Interval'.
#>  "ANY#Interval" would also be valid
#> Error in round_x - lhs: Arithmetic operators undefined for 'Interval' and 'Interval' classes:
#>   convert one to numeric or a matching time-span class.
tbl %>% select(everything())
#> Error in round_x - lhs: Arithmetic operators undefined for 'Interval' and 'Interval' classes:
#>   convert one to numeric or a matching time-span class.
tbl %>% arrange(end)
#> Error in round_x - lhs: Arithmetic operators undefined for 'Interval' and 'Interval' classes:
#>   convert one to numeric or a matching time-span class.
tbl %>% filter(x > 5)
#> Error in round_x - lhs: Arithmetic operators undefined for 'Interval' and 'Interval' classes:
#>   convert one to numeric or a matching time-span class.

Note that one verb that does work is removing the interval column, and once it is gone all the above verbs work as expected:

tbl %>% select(-interval)
#> # A tibble: 10 x 3
#>        x start               end                
#>    <int> <dttm>              <dttm>             
#>  1     1 2018-01-01 12:00:00 2018-01-01 15:00:00
#>  2     2 2018-01-04 22:40:00 2018-01-05 01:40:00
#>  3     3 2018-01-08 09:20:00 2018-01-08 12:20:00
#>  4     4 2018-01-11 20:00:00 2018-01-11 23:00:00
#>  5     5 2018-01-15 06:40:00 2018-01-15 09:40:00
#>  6     6 2018-01-18 17:20:00 2018-01-18 20:20:00
#>  7     7 2018-01-22 04:00:00 2018-01-22 07:00:00
#>  8     8 2018-01-25 14:40:00 2018-01-25 17:40:00
#>  9     9 2018-01-29 01:20:00 2018-01-29 04:20:00
#> 10    10 2018-02-01 12:00:00 2018-02-01 15:00:00
tbl %>% select(-interval) %>% mutate(y = 1:10)
#> # A tibble: 10 x 4
#>        x start               end                     y
#>    <int> <dttm>              <dttm>              <int>
#>  1     1 2018-01-01 12:00:00 2018-01-01 15:00:00     1
#>  2     2 2018-01-04 22:40:00 2018-01-05 01:40:00     2
#>  3     3 2018-01-08 09:20:00 2018-01-08 12:20:00     3
#>  4     4 2018-01-11 20:00:00 2018-01-11 23:00:00     4
#>  5     5 2018-01-15 06:40:00 2018-01-15 09:40:00     5
#>  6     6 2018-01-18 17:20:00 2018-01-18 20:20:00     6
#>  7     7 2018-01-22 04:00:00 2018-01-22 07:00:00     7
#>  8     8 2018-01-25 14:40:00 2018-01-25 17:40:00     8
#>  9     9 2018-01-29 01:20:00 2018-01-29 04:20:00     9
#> 10    10 2018-02-01 12:00:00 2018-02-01 15:00:00    10
tbl %>% select(-interval) %>% select(everything())
#> # A tibble: 10 x 3
#>        x start               end                
#>    <int> <dttm>              <dttm>             
#>  1     1 2018-01-01 12:00:00 2018-01-01 15:00:00
#>  2     2 2018-01-04 22:40:00 2018-01-05 01:40:00
#>  3     3 2018-01-08 09:20:00 2018-01-08 12:20:00
#>  4     4 2018-01-11 20:00:00 2018-01-11 23:00:00
#>  5     5 2018-01-15 06:40:00 2018-01-15 09:40:00
#>  6     6 2018-01-18 17:20:00 2018-01-18 20:20:00
#>  7     7 2018-01-22 04:00:00 2018-01-22 07:00:00
#>  8     8 2018-01-25 14:40:00 2018-01-25 17:40:00
#>  9     9 2018-01-29 01:20:00 2018-01-29 04:20:00
#> 10    10 2018-02-01 12:00:00 2018-02-01 15:00:00
tbl %>% select(-interval) %>% arrange(end)
#> # A tibble: 10 x 3
#>        x start               end                
#>    <int> <dttm>              <dttm>             
#>  1     1 2018-01-01 12:00:00 2018-01-01 15:00:00
#>  2     2 2018-01-04 22:40:00 2018-01-05 01:40:00
#>  3     3 2018-01-08 09:20:00 2018-01-08 12:20:00
#>  4     4 2018-01-11 20:00:00 2018-01-11 23:00:00
#>  5     5 2018-01-15 06:40:00 2018-01-15 09:40:00
#>  6     6 2018-01-18 17:20:00 2018-01-18 20:20:00
#>  7     7 2018-01-22 04:00:00 2018-01-22 07:00:00
#>  8     8 2018-01-25 14:40:00 2018-01-25 17:40:00
#>  9     9 2018-01-29 01:20:00 2018-01-29 04:20:00
#> 10    10 2018-02-01 12:00:00 2018-02-01 15:00:00
tbl %>% select(-interval) %>% filter(x > 5)
#> # A tibble: 5 x 3
#>       x start               end                
#>   <int> <dttm>              <dttm>             
#> 1     6 2018-01-18 17:20:00 2018-01-18 20:20:00
#> 2     7 2018-01-22 04:00:00 2018-01-22 07:00:00
#> 3     8 2018-01-25 14:40:00 2018-01-25 17:40:00
#> 4     9 2018-01-29 01:20:00 2018-01-29 04:20:00
#> 5    10 2018-02-01 12:00:00 2018-02-01 15:00:00

Created on 2018-02-08 by the reprex package (v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.3 (2017-11-30)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United Kingdom.1252 
#>  tz       America/Los_Angeles         
#>  date     2018-02-08
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                            
#>  assertthat   0.2.0      2017-04-11 CRAN (R 3.4.2)                    
#>  backports    1.1.2      2017-12-13 CRAN (R 3.4.3)                    
#>  base       * 3.4.3      2017-12-06 local                             
#>  bindr        0.1        2016-11-13 CRAN (R 3.4.2)                    
#>  bindrcpp   * 0.2        2017-06-17 CRAN (R 3.4.2)                    
#>  broom        0.4.3      2017-11-20 CRAN (R 3.4.3)                    
#>  cellranger   1.1.0      2016-07-27 CRAN (R 3.4.2)                    
#>  cli          1.0.0      2017-11-05 CRAN (R 3.4.2)                    
#>  colorspace   1.3-2      2016-12-14 CRAN (R 3.4.2)                    
#>  compiler     3.4.3      2017-12-06 local                             
#>  crayon       1.3.4      2017-09-16 CRAN (R 3.4.2)                    
#>  datasets   * 3.4.3      2017-12-06 local                             
#>  devtools     1.13.4     2017-11-09 CRAN (R 3.4.2)                    
#>  digest       0.6.15     2018-01-28 CRAN (R 3.4.3)                    
#>  dplyr      * 0.7.4      2017-09-28 CRAN (R 3.4.2)                    
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.4.2)                    
#>  forcats    * 0.2.0      2017-01-23 CRAN (R 3.4.2)                    
#>  foreign      0.8-69     2017-06-22 CRAN (R 3.4.3)                    
#>  ggplot2    * 2.2.1.9000 2018-02-06 Github (tidyverse/ggplot2@a2dc248)
#>  glue         1.2.0      2017-10-29 CRAN (R 3.4.2)                    
#>  graphics   * 3.4.3      2017-12-06 local                             
#>  grDevices  * 3.4.3      2017-12-06 local                             
#>  grid         3.4.3      2017-12-06 local                             
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.4.2)                    
#>  haven        1.1.1      2018-01-18 CRAN (R 3.4.3)                    
#>  hms          0.4.1      2018-01-24 CRAN (R 3.4.3)                    
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.4.2)                    
#>  httr         1.3.1      2017-08-20 CRAN (R 3.4.2)                    
#>  jsonlite     1.5        2017-06-01 CRAN (R 3.4.2)                    
#>  knitr        1.19       2018-01-29 CRAN (R 3.4.3)                    
#>  lattice      0.20-35    2017-03-25 CRAN (R 3.4.3)                    
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.4.2)                    
#>  lubridate  * 1.7.2      2018-02-06 CRAN (R 3.4.3)                    
#>  magrittr     1.5        2014-11-22 CRAN (R 3.4.2)                    
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.4.2)                    
#>  methods    * 3.4.3      2017-12-06 local                             
#>  mnormt       1.5-5      2016-10-15 CRAN (R 3.4.1)                    
#>  modelr       0.1.1      2017-07-24 CRAN (R 3.4.2)                    
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.4.2)                    
#>  nlme         3.1-131    2017-02-06 CRAN (R 3.4.3)                    
#>  parallel     3.4.3      2017-12-06 local                             
#>  pillar       1.1.0      2018-01-14 CRAN (R 3.4.3)                    
#>  pkgconfig    2.0.1      2017-03-21 CRAN (R 3.4.2)                    
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.4.2)                    
#>  psych        1.7.8      2017-09-09 CRAN (R 3.4.2)                    
#>  purrr      * 0.2.4      2017-10-18 CRAN (R 3.4.2)                    
#>  R6           2.2.2      2017-06-17 CRAN (R 3.4.2)                    
#>  Rcpp         0.12.15    2018-01-20 CRAN (R 3.4.3)                    
#>  readr      * 1.1.1      2017-05-16 CRAN (R 3.4.2)                    
#>  readxl       1.0.0      2017-04-18 CRAN (R 3.4.2)                    
#>  reshape2     1.4.3      2017-12-11 CRAN (R 3.4.3)                    
#>  rlang        0.1.6.9003 2018-02-09 Github (tidyverse/rlang@616fd4d)  
#>  rmarkdown    1.8        2017-11-17 CRAN (R 3.4.3)                    
#>  rprojroot    1.3-2      2018-01-03 CRAN (R 3.4.3)                    
#>  rvest        0.3.2      2016-06-17 CRAN (R 3.4.2)                    
#>  scales       0.5.0.9000 2018-01-24 Github (hadley/scales@d767915)    
#>  stats      * 3.4.3      2017-12-06 local                             
#>  stringi      1.1.6      2017-11-17 CRAN (R 3.4.2)                    
#>  stringr    * 1.2.0      2017-02-18 CRAN (R 3.4.2)                    
#>  tibble     * 1.4.2      2018-01-22 CRAN (R 3.4.3)                    
#>  tidyr      * 0.8.0      2018-01-29 CRAN (R 3.4.3)                    
#>  tidyverse  * 1.2.1      2017-11-14 CRAN (R 3.4.3)                    
#>  tools        3.4.3      2017-12-06 local                             
#>  utf8         1.1.3      2018-01-03 CRAN (R 3.4.3)                    
#>  utils      * 3.4.3      2017-12-06 local                             
#>  withr        2.1.1.9000 2018-01-24 Github (jimhester/withr@df18523)  
#>  xml2         1.2.0      2018-01-24 CRAN (R 3.4.3)                    
#>  yaml         2.1.16     2017-12-12 CRAN (R 3.4.3)
@cderv
Copy link
Contributor

cderv commented Feb 9, 2018

There is still an open issue in dplyr related to this I think. tidyverse/dplyr#2432
Seems to me it is a dplyr or tibble issue

@vspinu
Copy link
Member

vspinu commented Feb 9, 2018

Right. If you search "intervals dplyr" on this repo it should give you further info.

@vspinu vspinu closed this as completed Feb 9, 2018
@Zedseayou
Copy link
Author

Got it - should have looked at the other repo first!

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

3 participants