Skip to content

time_type is getting nulled (possibly with rename) #682

@JavierMtzRdz

Description

@JavierMtzRdz

time_type gets nulled when (possibly) using rename. In my initial attempt to reproduce the error, it appears to work fine.

devtools::load_all(".")
#> ℹ Loading epiprocess
#> Loading required package: epidatasets
#> 
#> Registered S3 method overwritten by 'tsibble':
#>   method               from 
#>   as_tibble.grouped_df dplyr
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> 
#> The following object is masked from 'package:epiprocess':
#> 
#>     between
#> 
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> 
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tibble)

#  Create weekly data but label it as 'daily' ----
dates <- seq(as.Date("2020-01-01"), by = "week", length.out = 10)
edf_weekly <- tibble(
  geo_value = "ca",
  age = c(rep("1-5", 10), rep(">=5", 10)),
  time_value = rep(dates, 2),
  value = 1:20
) %>%
  as_epi_df(other_keys = "age")

## Verify initial metadata
print(attr(edf_weekly, "metadata"))
#> $geo_type
#> [1] "state"
#> 
#> $time_type
#> [1] "week"
#> 
#> $as_of
#> [1] "2025-12-17 17:37:43 PST"
#> 
#> $other_keys
#> [1] "age"

## Force time_type to 'day'
attr(edf_weekly, "metadata")$time_type <- "day"

## epi_slide on the original object
edf_weekly |>
  epi_slide(cases_7sd = mean(value, na.rm = TRUE), .window_size = 14)
#> An `epi_df` object, 20 x 5 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * other_keys = age
#> * as_of     = 2025-12-17 17:37:43.695804
#> 
#> # A tibble: 20 × 5
#>    geo_value age   time_value value cases_7sd
#>    <chr>     <chr> <date>     <int>     <dbl>
#>  1 ca        1-5   2020-01-01     1       1  
#>  2 ca        1-5   2020-01-08     2       1.5
#>  3 ca        1-5   2020-01-15     3       2.5
#>  4 ca        1-5   2020-01-22     4       3.5
#>  5 ca        1-5   2020-01-29     5       4.5
#>  6 ca        1-5   2020-02-05     6       5.5
#>  7 ca        1-5   2020-02-12     7       6.5
#>  8 ca        1-5   2020-02-19     8       7.5
#>  9 ca        1-5   2020-02-26     9       8.5
#> 10 ca        1-5   2020-03-04    10       9.5
#> 11 ca        >=5   2020-01-01    11      11  
#> 12 ca        >=5   2020-01-08    12      11.5
#> 13 ca        >=5   2020-01-15    13      12.5
#> 14 ca        >=5   2020-01-22    14      13.5
#> 15 ca        >=5   2020-01-29    15      14.5
#> 16 ca        >=5   2020-02-05    16      15.5
#> 17 ca        >=5   2020-02-12    17      16.5
#> 18 ca        >=5   2020-02-19    18      17.5
#> 19 ca        >=5   2020-02-26    19      18.5
#> 20 ca        >=5   2020-03-04    20      19.5

Works well

# Test Rename (Ungrouped) ---
## 'value' -> 'val'

(res_select <- edf_weekly %>% rename(val = value))
#> An `epi_df` object, 20 x 4 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * other_keys = age
#> * as_of     = 2025-12-17 17:37:43.695804
#> 
#> # A tibble: 20 × 4
#>    geo_value age   time_value   val
#>  * <chr>     <chr> <date>     <int>
#>  1 ca        1-5   2020-01-01     1
#>  2 ca        1-5   2020-01-08     2
#>  3 ca        1-5   2020-01-15     3
#>  4 ca        1-5   2020-01-22     4
#>  5 ca        1-5   2020-01-29     5
#>  6 ca        1-5   2020-02-05     6
#>  7 ca        1-5   2020-02-12     7
#>  8 ca        1-5   2020-02-19     8
#>  9 ca        1-5   2020-02-26     9
#> 10 ca        1-5   2020-03-04    10
#> 11 ca        >=5   2020-01-01    11
#> 12 ca        >=5   2020-01-08    12
#> 13 ca        >=5   2020-01-15    13
#> 14 ca        >=5   2020-01-22    14
#> 15 ca        >=5   2020-01-29    15
#> 16 ca        >=5   2020-02-05    16
#> 17 ca        >=5   2020-02-12    17
#> 18 ca        >=5   2020-02-19    18
#> 19 ca        >=5   2020-02-26    19
#> 20 ca        >=5   2020-03-04    20

Correctly renamed

## Check if epi_slide still works on the renamed object
res_select |>
  epi_slide(cases_7sd = mean(val, na.rm = TRUE), .window_size = 14)
#> An `epi_df` object, 20 x 5 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * other_keys = age
#> * as_of     = 2025-12-17 17:37:43.695804
#> 
#> # A tibble: 20 × 5
#>    geo_value age   time_value   val cases_7sd
#>    <chr>     <chr> <date>     <int>     <dbl>
#>  1 ca        1-5   2020-01-01     1       1  
#>  2 ca        1-5   2020-01-08     2       1.5
#>  3 ca        1-5   2020-01-15     3       2.5
#>  4 ca        1-5   2020-01-22     4       3.5
#>  5 ca        1-5   2020-01-29     5       4.5
#>  6 ca        1-5   2020-02-05     6       5.5
#>  7 ca        1-5   2020-02-12     7       6.5
#>  8 ca        1-5   2020-02-19     8       7.5
#>  9 ca        1-5   2020-02-26     9       8.5
#> 10 ca        1-5   2020-03-04    10       9.5
#> 11 ca        >=5   2020-01-01    11      11  
#> 12 ca        >=5   2020-01-08    12      11.5
#> 13 ca        >=5   2020-01-15    13      12.5
#> 14 ca        >=5   2020-01-22    14      13.5
#> 15 ca        >=5   2020-01-29    15      14.5
#> 16 ca        >=5   2020-02-05    16      15.5
#> 17 ca        >=5   2020-02-12    17      16.5
#> 18 ca        >=5   2020-02-19    18      17.5
#> 19 ca        >=5   2020-02-26    19      18.5
#> 20 ca        >=5   2020-03-04    20      19.5

Still working

## Rename other key 'age' -> 'age_group'

(res_select <- edf_weekly %>% rename(age_group = age))
#> An `epi_df` object, 20 x 4 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * other_keys = age_group
#> * as_of     = 2025-12-17 17:37:43.695804
#> 
#> # A tibble: 20 × 4
#>    geo_value age_group time_value value
#>  * <chr>     <chr>     <date>     <int>
#>  1 ca        1-5       2020-01-01     1
#>  2 ca        1-5       2020-01-08     2
#>  3 ca        1-5       2020-01-15     3
#>  4 ca        1-5       2020-01-22     4
#>  5 ca        1-5       2020-01-29     5
#>  6 ca        1-5       2020-02-05     6
#>  7 ca        1-5       2020-02-12     7
#>  8 ca        1-5       2020-02-19     8
#>  9 ca        1-5       2020-02-26     9
#> 10 ca        1-5       2020-03-04    10
#> 11 ca        >=5       2020-01-01    11
#> 12 ca        >=5       2020-01-08    12
#> 13 ca        >=5       2020-01-15    13
#> 14 ca        >=5       2020-01-22    14
#> 15 ca        >=5       2020-01-29    15
#> 16 ca        >=5       2020-02-05    16
#> 17 ca        >=5       2020-02-12    17
#> 18 ca        >=5       2020-02-19    18
#> 19 ca        >=5       2020-02-26    19
#> 20 ca        >=5       2020-03-04    20

Correctly renamed

## Check if epi_slide still works on the renamed object
res_select |>
  epi_slide(cases_7sd = mean(value, na.rm = TRUE), .window_size = 14)
#> An `epi_df` object, 20 x 5 with metadata:
#> * geo_type  = state
#> * time_type = day
#> * other_keys = age_group
#> * as_of     = 2025-12-17 17:37:43.695804
#> 
#> # A tibble: 20 × 5
#>    geo_value age_group time_value value cases_7sd
#>    <chr>     <chr>     <date>     <int>     <dbl>
#>  1 ca        1-5       2020-01-01     1       1  
#>  2 ca        1-5       2020-01-08     2       1.5
#>  3 ca        1-5       2020-01-15     3       2.5
#>  4 ca        1-5       2020-01-22     4       3.5
#>  5 ca        1-5       2020-01-29     5       4.5
#>  6 ca        1-5       2020-02-05     6       5.5
#>  7 ca        1-5       2020-02-12     7       6.5
#>  8 ca        1-5       2020-02-19     8       7.5
#>  9 ca        1-5       2020-02-26     9       8.5
#> 10 ca        1-5       2020-03-04    10       9.5
#> 11 ca        >=5       2020-01-01    11      11  
#> 12 ca        >=5       2020-01-08    12      11.5
#> 13 ca        >=5       2020-01-15    13      12.5
#> 14 ca        >=5       2020-01-22    14      13.5
#> 15 ca        >=5       2020-01-29    15      14.5
#> 16 ca        >=5       2020-02-05    16      15.5
#> 17 ca        >=5       2020-02-12    17      16.5
#> 18 ca        >=5       2020-02-19    18      17.5
#> 19 ca        >=5       2020-02-26    19      18.5
#> 20 ca        >=5       2020-03-04    20      19.5

Still working

# Test Rename grouped ----
## Grouping by geo_value
edf_grouped <- edf_weekly %>% group_by(geo_value, age)

## Verify grouped metadata and class
attr(edf_grouped, "metadata")
print(class(edf_grouped)


## Rename on grouped data
res_select_grouped <- edf_grouped %>% rename(val = value)

# Check if sliding works after grouped rename
res_select_grouped |>
  epi_slide(cases_7sd = mean(val, na.rm = TRUE), .window_size = 14)
#> Error in parse(text = input): <text>:11:1: unexpected symbol
#> 10: ## Rename on grouped data
#> 11: res_select_grouped
#>     ^

Work as expected

# Final check of metadata
attr(res_select_grouped, "metadata")
#> Error: object 'res_select_grouped' not found

Created on 2025-12-17 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions