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

Unexpected behaviour when filtering by week #28

Open
jpolonsky opened this issue Jul 22, 2019 · 1 comment
Open

Unexpected behaviour when filtering by week #28

jpolonsky opened this issue Jul 22, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@jpolonsky
Copy link

I've noticed some unusual/unexpected behaviour when trying to filter by multiple weeks.

library(tidyverse)
library(aweek)
df <- 
  tibble(
    x = rnorm(61),
    date = Sys.Date() + (-30:30)
  ) %>% 
  mutate(
    epiweek = date2week(date, floor_day = TRUE)
  )

df
#> # A tibble: 61 x 3
#>          x date       epiweek 
#>      <dbl> <date>     <aweek> 
#>  1  0.932  2019-06-22 2019-W25
#>  2 -0.108  2019-06-23 2019-W25
#>  3  0.443  2019-06-24 2019-W26
#>  4 -0.0255 2019-06-25 2019-W26
#>  5  0.586  2019-06-26 2019-W26
#>  6  0.268  2019-06-27 2019-W26
#>  7 -0.783  2019-06-28 2019-W26
#>  8  0.682  2019-06-29 2019-W26
#>  9  0.790  2019-06-30 2019-W26
#> 10  0.700  2019-07-01 2019-W27
#> # … with 51 more rows

df %>% filter(epiweek %in% c("2019-W25", "2019-W26"))
#> # A tibble: 9 x 3
#>         x date       epiweek 
#>     <dbl> <date>     <aweek> 
#> 1  0.932  2019-06-22 2019-W25
#> 2 -0.108  2019-06-23 2019-W25
#> 3  0.443  2019-06-24 2019-W26
#> 4 -0.0255 2019-06-25 2019-W26
#> 5  0.586  2019-06-26 2019-W26
#> 6  0.268  2019-06-27 2019-W26
#> 7 -0.783  2019-06-28 2019-W26
#> 8  0.682  2019-06-29 2019-W26
#> 9  0.790  2019-06-30 2019-W26

first_week <- date2week("2019-06-22", floor_day = TRUE)
last_week <- date2week("2019-06-24", floor_day = TRUE)

df %>% filter(epiweek %in% first_week)
#> # A tibble: 2 x 3
#>        x date       epiweek 
#>    <dbl> <date>     <aweek> 
#> 1  0.932 2019-06-22 2019-W25
#> 2 -0.108 2019-06-23 2019-W25
df %>% filter(epiweek %in% last_week)
#> # A tibble: 7 x 3
#>         x date       epiweek 
#>     <dbl> <date>     <aweek> 
#> 1  0.443  2019-06-24 2019-W26
#> 2 -0.0255 2019-06-25 2019-W26
#> 3  0.586  2019-06-26 2019-W26
#> 4  0.268  2019-06-27 2019-W26
#> 5 -0.783  2019-06-28 2019-W26
#> 6  0.682  2019-06-29 2019-W26
#> 7  0.790  2019-06-30 2019-W26
df %>% filter(epiweek %in% c(first_week, last_week))
#> # A tibble: 0 x 3
#> # … with 3 variables: x <dbl>, date <date>, epiweek <aweek>

Created on 2019-07-22 by the reprex package (v0.3.0)

As you can see, the first few filters work, but not when combined in c(), but this would be really helpful (essential) to work with weeks in combination

@zkamvar
Copy link
Member

zkamvar commented Jul 22, 2019

Quick solution is to use trunc() to ensure that the day element is removed:

df %>% filter(epiweek %in% trunc(c(first_week, last_week))

@zkamvar zkamvar added the bug Something isn't working label Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants