Search code examples
ranytime

anytime producing NA dates only for dates before the 10th


I am attempting to read dates from a CSV. Sample:

Date    User 1  User 2
8/1/2019    IN  IN
8/2/2019    IN  Out
8/3/2019    IN  IN
8/4/2019    IN  IN
8/5/2019    IN  IN
8/6/2019    IN  IN
8/7/2019    IN  IN
8/8/2019    IN  IN
8/9/2019    IN  IN
8/10/2019   IN  IN
8/11/2019   IN  IN

I thought I had a good method worked out for reading these dates correctly, which was:

Vacation <- read.csv("Vacation.csv", stringsAsFactors = FALSE)
Vacation$Date <- anydate(Vacation$Date)

However, for some reason only dates before the 10th are NA once I convert to date.

 [1] NA           NA           NA           NA           NA           NA          
   [7] NA           NA           NA           "2019-08-10" "2019-08-11" "2019-08-12"
  [13] "2019-08-13" "2019-08-14" "2019-08-15" "2019-08-16" "2019-08-17" "2019-08-18"
  [19] "2019-08-19" "2019-08-20" "2019-08-21" "2019-08-22" "2019-08-23" "2019-08-24"
  [25] "2019-08-25" "2019-08-26" "2019-08-27" "2019-08-28" "2019-08-29" "2019-08-30"
  [31] "2019-08-31" NA           NA           NA           NA           NA          
  [37] NA           NA           NA           NA           "2019-09-10" "2019-09-11"
  [43] "2019-09-12" "2019-09-13" "2019-09-14" "2019-09-15" "2019-09-16" "2019-09-17"
  [49] "2019-09-18" "2019-09-19" "2019-09-20" "2019-09-21" "2019-09-22" "2019-09-23"
  [55] "2019-09-24" "2019-09-25" "2019-09-26" "2019-09-27" "2019-09-28" "2019-09-29"

Solution

  • Base R

    as.Date(strptime(d$Date, "%m/%d/%Y"))
    

    OR

    lubridate::mdy(d$Date)
    #[1] "2019-08-01" "2019-08-02" "2019-08-03" "2019-08-04" "2019-08-05" "2019-08-06" "2019-08-07"
    #[8] "2019-08-08" "2019-08-09" "2019-08-10" "2019-08-11"