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:
.fmust 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
case_when()is a good alternative.