Error in dates/ times

Epi R training exercise - Posting your reprex

Simulated error -
I am receiving the following error message, and was wondering if anyone may be able to provide assistance.

Error message:
Error in unsupported_date_time(time) :
Unsupported date-time class 'yearweek’Unsupported date-time class ‘vctrs_vctr’

Example code below:

pacman::p_load(
 readxl,       # import data direct 
 rio,          # for importing data
 here,         # for locating files
 janitor,      # for data cleaning  
 lubridate,    # for date cleaning
 epikit,       # for easy inline code
 scales,       # for percents and date labels
 officer,      # border lines
 flextable,    # make HTML tables
 gtsummary,    # for nice tables
 apyramid,     # for age/sex pyramids
 RColorBrewer, # for colour palettes
 gghighlight,  # highlighting plot parts  
 ggExtra,      # special plotting functions
 viridis,      # for color-blind friendly color scales
 tsibble,      # for epiweeks and other time series analyses
 tidyverse,# for data management and visualization
 datapasta, 
 reprex)



cases <- data.frame(
       report_dt = c("2020-09-09","2020-07-25",
                     "2020-07-02","2020-11-05","2020-09-26"),
      week_onset = c("2020 W37","2020 W30",
                     "2020 W27","2020 W45","2020 W39"),
 week_onset_date = c("2020-09-07","2020-07-20",
                     "2020-06-29","2020-11-02","2020-09-21"))  
cases <- cases %>% 
 mutate(week_onset  = yearweek(report_dt, week_start = 1),week_onset_date = as.Date(week_onset))

all_weeks <- seq.Date(
                from = floor_date(min(cases$week_onset, na.rm = TRUE), unit = "weeks"),
                to = ceiling_date(max(cases$week_onset, na.rm = TRUE), unit = " weeks"),
                by = "weeks") 

Please find additional session info below

Thank you for your help.

1 Like

hi @francesca.bell and welcome to the forum!

The floor_date() function from lubridate expects a date value (e.g. “2014-12-01”) not a week value (e.g. “2020 W37”). So to properly create your vector of dates, point the floor_date() function to the column that is based on the epiweek but structured as dates: week_onset_date:

I hope this helps!

pacman::p_load(
  readxl,       # import data direct 
  rio,          # for importing data
  here,         # for locating files
  janitor,      # for data cleaning  
  lubridate,    # for date cleaning
  epikit,       # for easy inline code
  scales,       # for percents and date labels
  officer,      # border lines
  flextable,    # make HTML tables
  gtsummary,    # for nice tables
  apyramid,     # for age/sex pyramids
  RColorBrewer, # for colour palettes
  gghighlight,  # highlighting plot parts  
  ggExtra,      # special plotting functions
  viridis,      # for color-blind friendly color scales
  tsibble,      # for epiweeks and other time series analyses
  tidyverse,# for data management and visualization
  datapasta, 
  reprex)

cases <- data.frame(
  report_dt = c("2020-09-09","2020-07-25",
                "2020-07-02","2020-11-05","2020-09-26"),
  week_onset = c("2020 W37","2020 W30",
                 "2020 W27","2020 W45","2020 W39"),
  week_onset_date = c("2020-09-07","2020-07-20",
                      "2020-06-29","2020-11-02","2020-09-21"))  
cases <- cases %>% 
  mutate(week_onset  = yearweek(report_dt, week_start = 1),week_onset_date = as.Date(week_onset))

all_weeks <- seq.Date(
  from = floor_date(min(cases$week_onset_date, na.rm = TRUE), unit = "weeks"),
  to = ceiling_date(max(cases$week_onset_date, na.rm = TRUE), unit = " weeks"),
  by = "weeks") 
1 Like