Dplyr: Strange behavior when filtering tibble containing lubridate column

Created on 11 Jul 2018  路  3Comments  路  Source: tidyverse/dplyr

Trying to filter a tibble with lubridate columns can result in strange behavior.

library(tidyverse)
library(tidyr)
library(lubridate)

htwd <- structure(list(code = c(10146L, 10252L, 10136L, 212L, 10252L, 
75L, 212L, 89L, 10134L, 349L), weekday = c(6, 6, 6, 6, 6, 6, 
6, 6, 6, 6), timedeb = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(8, 
11, 8, 22, 17, 19, 5, 3, 18, 22), minute = c(45, 0, 30, 15, 30, 
45, 45, 30, 15, 30), class = structure("Period", package = "lubridate"))), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("code", 
"weekday", "timedeb"))

ftwd <- htwd %>% filter(code == 212)

ftwd$timedeb # This column has 10 elements (altough length(ftwd$timedeb) yields 3)
ftwd$code # This column has 3 elements
ftwd %>% mutate(x = weekday * period(day = 1) + timedeb) # As a result, this line yields an error

ftwd <- htwd[htwd$code == 212, ] # Good old subsetting gives the expected result
ftwd$timedeb
ftwd$code
ftwd %>% mutate(x = weekday * period(day = 1) + timedeb)

Most helpful comment

I think this has already been "resolved" in dplyr 0.7.6 by adding an error message when filtering and lubridate column is present. By resolved, i think it means that it will be dealt later, in vctrs package. For now, now lubridate support.

library(dplyr, warn.conflicts = FALSE)
packageVersion("dplyr")
#> [1] '0.7.6'

htwd <- structure(list(code = c(10146L, 10252L, 10136L, 212L, 10252L, 
                                75L, 212L, 89L, 10134L, 349L), weekday = c(6, 6, 6, 6, 6, 6, 
                                                                           6, 6, 6, 6), timedeb = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                              0), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 
                                                                                                                                                                    0, 0, 0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(8, 
                                                                                                                                                                                                                                          11, 8, 22, 17, 19, 5, 3, 18, 22), minute = c(45, 0, 30, 15, 30, 
                                                                                                                                                                                                                                                                                       45, 45, 30, 15, 30), class = structure("Period", package = "lubridate"))), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                -10L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("code", 
                                                                                                                                                                                                                                                                                                                                                                                                                                            "weekday", "timedeb"))

htwd %>% 
  filter(code == 212)
#> Error in filter_impl(.data, quo): Column `timedeb` classes Period and Interval from lubridate are currently not supported.

Created on 2018-07-11 by the reprex package (v0.2.0).

All 3 comments

I think this has already been "resolved" in dplyr 0.7.6 by adding an error message when filtering and lubridate column is present. By resolved, i think it means that it will be dealt later, in vctrs package. For now, now lubridate support.

library(dplyr, warn.conflicts = FALSE)
packageVersion("dplyr")
#> [1] '0.7.6'

htwd <- structure(list(code = c(10146L, 10252L, 10136L, 212L, 10252L, 
                                75L, 212L, 89L, 10134L, 349L), weekday = c(6, 6, 6, 6, 6, 6, 
                                                                           6, 6, 6, 6), timedeb = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                              0), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), month = c(0, 0, 0, 
                                                                                                                                                                    0, 0, 0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(8, 
                                                                                                                                                                                                                                          11, 8, 22, 17, 19, 5, 3, 18, 22), minute = c(45, 0, 30, 15, 30, 
                                                                                                                                                                                                                                                                                       45, 45, 30, 15, 30), class = structure("Period", package = "lubridate"))), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                -10L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("code", 
                                                                                                                                                                                                                                                                                                                                                                                                                                            "weekday", "timedeb"))

htwd %>% 
  filter(code == 212)
#> Error in filter_impl(.data, quo): Column `timedeb` classes Period and Interval from lubridate are currently not supported.

Created on 2018-07-11 by the reprex package (v0.2.0).

Thanks Christophe!

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/

Was this page helpful?
0 / 5 - 0 ratings