Issue:
I am working with onset dates. I need to convert the onset_date variable from character format to the Date class so that it can be used to create an epidemic curve. After using ymd(), the column class changes to Date, but all the values become NA. I also receive the warning: “All formats failed to parse. No formats found.”
Steps taken to find an answer:
I reviewed the Epi R Handbook information about R projects and working with dates. I also checked the class and range of the cleaned date variable to identify where the problem occurred. The original values are written with slashes, but I am unsure why ymd() cannot interpret them or which date conversion function should be used.
Example R code:
pacman::p_load(
rio,
here,
janitor,
tidyverse,
reprex,
datapasta
)
# Create a minimal synthetic version
surv_raw <- data.frame(
stringsAsFactors = FALSE,
case_id = c(
"694928",
"86340d",
"92d002",
"544bd1",
"6056ba"
),
sex = c("m", "f", "f", "f", "f"),
onset_date = c(
"11/9/2014",
"10/30/2014",
"8/16/2014",
"8/29/2014",
"10/20/2014"
)
)
# Trying to convert the column to class Date
surv_clean <- surv_raw %>%
clean_names() %>%
mutate(onset_date = ymd(onset_date))
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `onset_date = ymd(onset_date)`.
#> Caused by warning:
#> ! All formats failed to parse. No formats found.
# Checking the cleaned date column class and range
class(surv_clean$onset_date)
#> [1] "Date"
range(surv_clean$onset_date)
#> [1] NA NA
Why are the original onset dates being converted to NA, even though the column class changes to Date? How should I modify the date conversion command so that these month/day/year values are interpreted correctly?
Thank you in advance for your help.