NB: This is an exercise on posting reproducible examples for a R training course
Hi,
I am working on a dataset on Ebola. I am cleaning the data and wanted to assign a date format to the column “Onset date”. All the values were converted to NA. Any help you can provide would be greatly appreciated! Thank you!
Reprex is below:
``` r
pacman::p_load(rio, janitor, tidyverse, datapasta, reprex)
# data
surv_raw <- data.frame(
stringsAsFactors = FALSE,
check.names = 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))
#> 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.
# check the CLEANED date column class and date range
class(surv_clean$onset_date)
#> [1] "Date"
range(surv_clean$onset_date)
#> [1] NA NA
Created on 2024-03-04 with reprex v2.0.2
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.3.2 (2023-10-31 ucrt)
#> os Windows 10 x64 (build 19045)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United Kingdom.utf8
#> ctype English_United Kingdom.utf8
#> tz Europe/London
#> date 2024-03-04
#> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.2)
#> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.2)
#> datapasta * 3.1.0 2020-01-17 [1] CRAN (R 4.3.3)
#> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.2)
#> dplyr * 1.1.3 2023-09-03 [1] CRAN (R 4.3.2)
#> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.2)
#> fansi 1.0.5 2023-10-08 [1] CRAN (R 4.3.2)
#> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.2)
#> forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.2)
#> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.2)
#> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.2)
#> ggplot2 * 3.4.4 2023-10-12 [1] CRAN (R 4.3.2)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.2)
#> gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.2)
#> hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.2)
#> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.2)
#> janitor * 2.2.0 2023-02-02 [1] CRAN (R 4.3.2)
#> knitr 1.45 2023-10-30 [1] CRAN (R 4.3.2)
#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2)
#> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.2)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.2)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.2)
#> pacman 0.5.1 2019-03-11 [1] CRAN (R 4.3.2)
#> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.2)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.2)
#> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.2)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.2)
#> readr * 2.1.4 2023-02-10 [1] CRAN (R 4.3.2)
#> reprex * 2.0.2 2022-08-17 [1] CRAN (R 4.3.2)
#> rio * 1.0.1 2023-09-19 [1] CRAN (R 4.3.2)
#> rlang 1.1.2 2023-11-04 [1] CRAN (R 4.3.2)
#> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.2)
#> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.2)
#> scales 1.2.1 2022-08-20 [1] CRAN (R 4.3.2)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.2)
#> snakecase 0.11.1 2023-08-27 [1] CRAN (R 4.3.2)
#> stringi 1.8.1 2023-11-13 [1] CRAN (R 4.3.2)
#> stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.2)
#> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.2)
#> tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.2)
#> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.2)
#> tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.2)
#> timechange 0.2.0 2023-01-11 [1] CRAN (R 4.3.2)
#> tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.2)
#> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.2)
#> vctrs 0.6.4 2023-10-12 [1] CRAN (R 4.3.2)
#> withr 2.5.2 2023-10-30 [1] CRAN (R 4.3.2)
#> xfun 0.41 2023-11-01 [1] CRAN (R 4.3.2)
#> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.2)
#>
#> [1] C:/Users/Magali.Collonnaz/AppData/Local/R/win-library/4.3
#> [2] C:/Program Files/R/R-4.3.2/library
#>
#> ──────────────────────────────────────────────────────────────────────────────