More on importing and exporting datetimes

Working with Dates and Times in R

Charlotte Wickham

Instructor

Fast parsing

parse_date_time() can be slow because it's designed to be forgiving and flexible.

library(fasttime)
fastPOSIXct("2003-02-27")
"2003-02-26 16:00:00 PST"
Working with Dates and Times in R

fast_strptime()

x <- "2001-02-27"

parse_date_time(x, order = "ymd")
"2001-02-27 UTC"
fast_strptime(x, format = "%Y-%m-%d")
"2001-02-27 UTC"
fast_strptime(x, format = "%y-%m-%d")
NA

See Details of format in strptime()

Working with Dates and Times in R

Exporting datetimes

library(tidyverse)
akl_hourly %>% 
  select(datetime) %>% 
  write_csv("tmp.csv")

tmp.csv

datetime
2016-01-01T00:00:00Z
2016-01-01T00:30:00Z
2016-01-01T01:00:00Z
2016-01-01T01:30:00Z
2016-01-01T02:00:00Z
2016-01-01T02:30:00Z
Working with Dates and Times in R

Formatting datetimes

my_stamp <- stamp("Tuesday October 10 2017")
Multiple formats matched: "%A %B %d %y%H"(1), "%A %B %y %d%H"(1), 
"%A %B %d %Y"(1), "%A October %m %y%d"(1), "%A October %m %Y"(0), 
"%A October %H %M%S"(1), "Tuesday %B %d %y%H"(1), "Tuesday %B %y %d%H"(1), 
"Tuesday %B %d %Y"(1), "Tuesday October %m %y%d"(1), 
"Tuesday October %m %Y"(1), "Tuesday October %H %M%S"(1)
Using: "%A %B %d %Y"
my_stamp(ymd("2003-02-27"))
"Thursday February 27 2003"
my_stamp
function(x) 
format(x, format = "%A %B %d %Y")
<environment: 0x1086ed780>
Working with Dates and Times in R

Let's practice!

Working with Dates and Times in R

Preparing Video For Download...