reconhub / aweek

Convert dates to arbitrary week definitions :calendar:
https://www.repidemicsconsortium.org/aweek
Other
17 stars 4 forks source link

Unexpected behaviour when filtering by week #28

Open jpolonsky opened 5 years ago

jpolonsky commented 5 years ago

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 commented 5 years ago

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

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