attempt to use fct_collapse() with class Date, must be factor or character vector, not an s3 object

65 Views Asked by At

I have a dataset I want to plot which requires some simplifying of the date which will be my x-axis. Right now I have every single day from March 2020 to November 2022, but I want to use manually defined groups of 6 month periods, with the leftover just being the exception (This is my first question here so let me know if more context is needed).

Anyways, my instinct was to use fct_collapse, but I get this error:

.f must be a factor or character vector, not an S3 object with class Date

I understand it is because my column: by_date_total$date is a date

I don't see a forcats operation that would work, is my only option to convert the date class and then reconvert it back to date? If I convert the date class, how will the the desired groups I set be read? I saw another answer which used as.date.frame to coerce the date class into a character class, but when I convert it to the character class I can no longer use ('%y-%m-%d - %y-%m-%d') BUT I guess it never worked in the first place.

my dataframe by_date_total:

date      total_deaths  total_cases
<date>       <dbl>       <dbl>
2020-03-15  68  3595        
2020-03-16  91  4502        
2020-03-17  117 5901        
2020-03-18  162 8345        
2020-03-19  212 12387       
2020-03-20  277 17998       
2020-03-21  359 24507   

This is what I tried that produced the error:

plot_by_date <- by_date_total%>%
  mutate(
    date2 = 
    fct_collapse(date, 
              '6 months' = c("2020-03-15" - "2020-09-14"),
              '12 months' =  c("2021-09-15" - "2021-03-14"),
              '18 months' = c("2022-03-15" - "2022-09-14"),
              '18 months+' = c("2022-09-15" - "2022-11-14"))
  )
  plot_by_date

I did not include the rest of the ggplot(aes()) info because I want to verify this step works first

changing it to character class idea: FOLLOWED BY RUNNING THE ABOVE AGAIN ERROR:

non-numeric argument to binary operator

plot_by_date <- as_data_frame(by_date_total) %>%
  rename(Date = date) %>%
  mutate(Date = str_replace_all(Date, "\\D", "-"),
         Date = as.character(Date))
plot_by_date
1

There are 1 best solutions below

0
M.Viking On

case_when() is a good alternative.

data.frame(date=as.Date(c("2020-03-16", "2020-03-14", "2021-09-16", "2022-03-16", "2022-09-16", "2022-11-15"))) %>% 
  mutate(date2 = case_when(date >= "2022-09-15" ~ "18+ months",
                           date >= "2022-03-15" ~ "18 months",
                           date >= "2021-09-15" ~ "12 months",
                           date >= "2020-03-15" ~ "6 months",
                           TRUE ~ "other"))

#        date      date2
#1 2020-03-16   6 months
#2 2020-03-14      other
#3 2021-09-16  12 months
#4 2022-03-16  18 months
#5 2022-09-16 18+ months
#6 2022-11-15 18+ months