Erasing excessive data when importing dates

Lets assume I have a syntax like this, I will repurpose the example in
Missing examples on how to use col_date(format = "...") · Issue #1203 · tidyverse/readr · GitHub :

example <- tibble::tribble(
               ~animal,        ~date,
                 "cow", "2021/05/05 9:03:45",
             "chicken", "2021/05/06 17:35:24",
               "horse", "2021/05/07 23:49:23",
                 "dog", "2021/05/08 05:05:59"
             )
write_csv(example, "date_example.csv")

Now I want to import these dates as dates, and I would expect something like

library(readr)
date_example <- read_csv("date_example.csv",
  col_types = cols(date = col_date(format = "%Y/%m/%d"))
)

to work, however, the timestamps at the end seem to get in the way. Trying it like this in my longer example results in a bunch of NAs. So how do I get rid of the "9:03:45" at the end in the first row for example?

You can just read it correctly:

date_example <- read_csv("date_example.csv",
                         col_types = cols(date = col_date(format = "%Y/%m/%d %H:%M:%S"))
)

with col_date() the time part gets removed anyway.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.