Fast parsing with fasttime
The fasttime package provides a single function fastPOSIXct(), designed to read in datetimes formatted according to ISO 8601. Because it only reads in one format, and doesn't have to guess a format, it is really fast!
You'll see how fast in this exercise by comparing how fast it reads in the dates from the Auckland hourly weather data (over 17,000 dates) to lubridates ymd_hms().
To compare run times you'll use the microbenchmark() function from the package of the same name. You pass in as many arguments as you want each being an expression to time.
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
We've loaded the datetimes from the Auckland hourly data as strings into the vector dates.
- Examine the structure of
datesto verify it is a string and in the ISO 8601 format. - Parse
dateswithfasttimeand pipe tostr()to verifyfastPOSIXctparses them correctly. - Now to compare timing, call
microbenchmarkwhere the first argument usesymd_hms()to parsedatesand the second usesfastPOSIXct().
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
library(microbenchmark)
library(fasttime)
# Examine structure of dates
str(___)
# Use fastPOSIXct() to parse dates
fastPOSIXct(___) %>% str()
# Compare speed of fastPOSIXct() to ymd_hms()
microbenchmark(
ymd_hms = ___(dates),
fasttime = ___(dates),
times = 20)