Hi everyone,
I was trying to clean a small dataset and convert the onset_date column to class Date using ymd() from lubridate. However, I got the following warning:
Warning message:
There was 1 warning in mutate().
In argument: onset_date = ymd(onset_date).
Caused by warning:
! All formats failed to parse. No formats found.
Could someone please explain why ymd() fails to parse these dates and how to fix it?
Hello,
Without a reproducible example, it’s hard to know for sure. I suspect it’s due to the dates in the data having a different format than the YYYY-mm-dd format that the ymd function expects. If your date is of the form mm-dd-YYYY then you should use the mdy function.
Here is an example:
# loading packages
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(tibble)
# creating fake data
fake_data <- tibble(
date_1 = c("2025-01-01", "2025-01-02", "2025-01-03", "2025-01-04", "2025-01-05"),
date_2 = c("01/01/25", "01/02/25", "01/03/25", "01/04/25", "01/05/25")
)
# converting the dates
fake_data |>
mutate(
date_1 = ymd(date_1),
date_2 = mdy(date_2)
)
#> # A tibble: 5 × 2
#> date_1 date_2
#> <date> <date>
#> 1 2025-01-01 2025-01-01
#> 2 2025-01-02 2025-01-02
#> 3 2025-01-03 2025-01-03
#> 4 2025-01-04 2025-01-04
#> 5 2025-01-05 2025-01-05
Created on 2025-10-16 with reprex v2.1.1
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.5.1 (2025-06-13)
#> os macOS Sequoia 15.6.1
#> system x86_64, darwin20
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/Toronto
#> date 2025-10-16
#> pandoc 3.6.3 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#> quarto 1.7.31 @ /usr/local/bin/quarto
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> cli 3.6.5 2025-04-23 [1] RSPM
#> digest 0.6.37 2024-08-19 [1] RSPM
#> dplyr * 1.1.4 2023-11-17 [1] RSPM
#> evaluate 1.0.5 2025-08-27 [1] RSPM
#> fastmap 1.2.0 2024-05-15 [1] RSPM
#> fs 1.6.6 2025-04-12 [1] RSPM
#> generics 0.1.4 2025-05-09 [1] RSPM
#> glue 1.8.0 2024-09-30 [1] RSPM
#> htmltools 0.5.8.1 2024-04-04 [1] RSPM
#> knitr 1.50 2025-03-16 [1] RSPM
#> lifecycle 1.0.4 2023-11-07 [1] RSPM
#> lubridate * 1.9.4 2024-12-08 [1] RSPM
#> magrittr 2.0.4 2025-09-12 [1] RSPM
#> pillar 1.11.1 2025-09-17 [1] RSPM
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM
#> R6 2.6.1 2025-02-15 [1] RSPM
#> reprex 2.1.1 2024-07-06 [1] RSPM
#> rlang 1.1.6 2025-04-11 [1] RSPM
#> rmarkdown 2.30 2025-09-28 [1] RSPM
#> rstudioapi 0.17.1 2024-10-22 [1] RSPM
#> sessioninfo 1.2.3 2025-02-05 [1] RSPM
#> tibble * 3.3.0 2025-06-08 [1] RSPM
#> tidyselect 1.2.1 2024-03-11 [1] RSPM
#> timechange 0.3.0 2024-01-18 [1] RSPM
#> vctrs 0.6.5 2023-12-01 [1] RSPM
#> withr 3.0.2 2024-10-28 [1] RSPM
#> xfun 0.53 2025-08-19 [1] RSPM
#> yaml 2.3.10 2024-07-26 [1] RSPM
#>
#> [1] /Users/timothychisamore/Library/R/x86_64/4.5/library
#> [2] /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/library
#> * ── Packages attached to the search path.
#>
#> ──────────────────────────────────────────────────────────────────────────────
All the best,
Tim
Thanks for the explanation! Here’s the reprex — I forgot to attach it to the post.
reprex 2
install and load packages
pacman::p_load(rio, here, janitor, tidyverse, reprex, datapasta)
import data
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”))
try to convert column to class “Date”
surv_clean ← surv_raw %>%
clean_names() %>%
mutate(onset_date = ymd(onset_date))
check the CLEANED date column class and date range
class(surv_clean$onset_date)
range(surv_clean$onset_date)
Hello,
Please see my previous response about the format of your date variable, that is what is causing your error.
All the best,
Tim
1 Like