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

Clash between dpylr::filter and lubridate #2520

Closed
FSpanhel opened this issue Mar 10, 2017 · 3 comments
Closed

Clash between dpylr::filter and lubridate #2520

FSpanhel opened this issue Mar 10, 2017 · 3 comments

Comments

@FSpanhel
Copy link

When I subset a dataframe with respect to a lubridate object using dplyr::filter I get unexpected results that do not occur when I subset using base commands.

library("dplyr")
library("lubridate")

periods <- c("00:00:00", "01:00:00")
periods <- hms(periods)
data <- data.frame(periods)

data_base <- data[hour(data$periods) < 1, , drop = F] # subset periods using base
data_dplyr <- dplyr::filter(data, hour(periods) < 1) # subset periods using filter
print(data_dplyr) # throws a warning and indicates that data_dplyr$periods equals periods[1]
#> Warning in format.data.frame(x, digits = digits, na.encode = FALSE):
#> corrupt data frame: columns will be truncated or padded with NAs
#>   periods
#> 1      0S

data_base$periods # expected output, equal to periods[1]
#> [1] "0S"
data_dplyr$periods # equals periods, i.e., subsetting did not work as expected
#> [1] "0S"       "1H 0M 0S"
@zeehio
Copy link
Contributor

zeehio commented Mar 10, 2017

When using base, the structure of the data frame seems fine:

str(data_base)
'data.frame':	1 obs. of  1 variable:
 $ periods:Formal class 'Period' [package "lubridate"] with 6 slots
  .. ..@ .Data : num 0
  .. ..@ year  : num 0
  .. ..@ month : num 0
  .. ..@ day   : num 0
  .. ..@ hour  : num 0
  .. ..@ minute: num 0

However if we see the structure of the dplyr data frame, we see that only one out of the 6 slots is subsetted:

str(data_dplyr)
'data.frame':	1 obs. of  1 variable:
 $ periods:Formal class 'Period' [package "lubridate"] with 6 slots
  .. ..@ .Data : num 0
  .. ..@ year  : num  0 0
  .. ..@ month : num  0 0
  .. ..@ day   : num  0 0
  .. ..@ hour  : num  0 1
  .. ..@ minute: num  0 0

This looks like a bug in dplyr

@zeehio
Copy link
Contributor

zeehio commented Mar 10, 2017

This is a problem with dplyr::slice_ as well:

With simple types it works:

dplyr::slice_(iris, 1)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa

In this case does not:

data <- data.frame(periods = hms(c("00:00:00", "01:00:00")))
dplyr::slice_(data, 1)
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

@hadley
Copy link
Member

hadley commented Mar 10, 2017

Duplicate of #2432

@hadley hadley closed this as completed Mar 10, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
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

3 participants