NA in date column

# EXAMPLE R SCRIPT FOR REPREX
# install and load packages
pacman::p_load(rio, janitor, tidyverse, here, datapasta, styler, reprex)
# import data
surv_raw <- import(here("data", "raw", "surveillance_linelist_20141201.csv"))
#> Error: No such file: C:/Users/anneofarrell/AppData/Local/Temp/Rtmpgzm9NL/reprex-526c3eb1a2e-extra-bass/data/raw/surveillance_linelist_20141201.csv

# try to convert column to class "Date"
surv_clean <- surv_raw %>% 
  clean_names() %>% 
  mutate(onset_date = ymd(onset_date))
#> Error in eval(expr, envir, enclos): object 'surv_raw' not found

# check the CLEANED date column class and date range
class(surv_clean$onset_date)
#> Error in eval(expr, envir, enclos): object 'surv_clean' not found
range(surv_clean$onset_date)
#> Error in eval(expr, envir, enclos): object 'surv_clean' not found

# create demo_data, by reducing surv_raw to 5 rows and 3 columns
demo_data <- surv_raw %>% 
  head(5) %>%                        # take the top 5 rows only
  select(case_id, sex, `onset date`) # keep only these columns
#> Error in eval(expr, envir, enclos): object 'surv_raw' not found

# 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))
#> 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
1 Like

Hi,

Please see the following thread for a solution to this problem: Exercise for R Training course: Unexpected NA value in date column - #2 by machupovirus

All the best,

Tim