Need support to convert dates

Hello,
I am trying to get the date column as class date. Some of the dates are converted others are converted into NA.
Could you please help me on this issue.
The following is my reprex:

pacman::p_load(rio, here, janitor, tidyverse, reprex, datapasta)

linelist <- data.frame(
  stringsAsFactors = FALSE,
  row.names = c("73","74","75","76","77",
                "78","79","80","81","82","83","84","85"),
  age = c(20, 14, 10, 13, 11, 2, 20, 1, 24, 18, 2, 35, 5),
  sexe = c("F","F","M","F","F","F",
           "M","F","F","M","F","F","F"),
  date_consult = c(NA,NA,NA,NA,"2025-05-20",
                   "2025-05-20","2025-05-20","2025-05-22","2025-05-25",
                   "2025-05-25",NA,NA,NA),
  date_sympt = c(NA,NA,NA,"2025-05-15",
                 "2025-05-16","2025-05-16","2025-05-14","2025-05-20",
                 "2025-05-22","2025-05-18","2025-05-25","2025-05-24",
                 "2025-05-29")
)

Thank you for your support
Created on 2025-06-05 with reprex v2.1.1

Hello,

I would use the lubridate package to achieve this, seen below:

# creating fake data
linelist <- data.frame(
  stringsAsFactors = FALSE,
  row.names = c(
    "73", "74", "75", "76", "77",
    "78", "79", "80", "81", "82", "83", "84", "85"
  ),
  age = c(20, 14, 10, 13, 11, 2, 20, 1, 24, 18, 2, 35, 5),
  sexe = c(
    "F", "F", "M", "F", "F", "F",
    "M", "F", "F", "M", "F", "F", "F"
  ),
  date_consult = c(
    NA, NA, NA, NA, "2025-05-20",
    "2025-05-20", "2025-05-20", "2025-05-22", "2025-05-25",
    "2025-05-25", NA, NA, NA
  ),
  date_sympt = c(
    NA, NA, NA, "2025-05-15",
    "2025-05-16", "2025-05-16", "2025-05-14", "2025-05-20",
    "2025-05-22", "2025-05-18", "2025-05-25", "2025-05-24",
    "2025-05-29"
  )
) |>
  tibble::as_tibble()

# converting to date format
linelist |>
  dplyr::mutate(
    date_consult = lubridate::ymd(date_consult),
    date_sympt = lubridate::ymd(date_sympt)
  )
#> # A tibble: 13 × 4
#>      age sexe  date_consult date_sympt
#>    <dbl> <chr> <date>       <date>    
#>  1    20 F     NA           NA        
#>  2    14 F     NA           NA        
#>  3    10 M     NA           NA        
#>  4    13 F     NA           2025-05-15
#>  5    11 F     2025-05-20   2025-05-16
#>  6     2 F     2025-05-20   2025-05-16
#>  7    20 M     2025-05-20   2025-05-14
#>  8     1 F     2025-05-22   2025-05-20
#>  9    24 F     2025-05-25   2025-05-22
#> 10    18 M     2025-05-25   2025-05-18
#> 11     2 F     NA           2025-05-25
#> 12    35 F     NA           2025-05-24
#> 13     5 F     NA           2025-05-29

Created on 2025-06-05 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.3 (2025-02-28)
#>  os       macOS Sequoia 15.5
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2025-06-05
#>  pandoc   3.4 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#>  quarto   1.3.353 @ /usr/local/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] RSPM (R 4.4.0)
#>  digest        0.6.37  2024-08-19 [1] RSPM (R 4.4.0)
#>  dplyr         1.1.4   2023-11-17 [1] RSPM (R 4.4.0)
#>  evaluate      1.0.3   2025-01-10 [1] RSPM (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] RSPM (R 4.4.0)
#>  fs            1.6.6   2025-04-12 [1] RSPM (R 4.4.0)
#>  generics      0.1.4   2025-05-09 [1] RSPM (R 4.4.0)
#>  glue          1.8.0   2024-09-30 [1] RSPM (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0)
#>  knitr         1.50    2025-03-16 [1] RSPM (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.4.0)
#>  lubridate     1.9.4   2024-12-08 [1] RSPM (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.4.0)
#>  pillar        1.10.2  2025-04-05 [1] RSPM (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.4.0)
#>  R6            2.6.1   2025-02-15 [1] RSPM (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] RSPM (R 4.4.0)
#>  rlang         1.1.6   2025-04-11 [1] RSPM (R 4.4.0)
#>  rmarkdown     2.29    2024-11-04 [1] RSPM (R 4.4.1)
#>  rstudioapi    0.17.1  2024-10-22 [1] RSPM (R 4.4.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] RSPM (R 4.4.0)
#>  tibble        3.2.1   2023-03-20 [1] RSPM (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] RSPM (R 4.4.0)
#>  timechange    0.3.0   2024-01-18 [1] RSPM (R 4.4.0)
#>  utf8          1.2.5   2025-05-01 [1] RSPM (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.4.0)
#>  withr         3.0.2   2024-10-28 [1] RSPM (R 4.4.0)
#>  xfun          0.52    2025-04-02 [1] RSPM (R 4.4.0)
#>  yaml          2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim

1 Like