1. More on importing and exporting datetimes
By now you are pretty good at getting dates and times into R, but there are a few other things you should know about, how to do that quickly, and how to output datetimes in a human readable format.
2. Fast parsing
You've seen how parse_date_time is a very flexible and forgiving way to import datetimes into R. But that ease of use comes with a cost, it can be a little slow. Why? Because behind the scenes, parse_date_time is doing a lot of work to interpret exactly how the dates are represented, and which format is being used for each date.
If your dates are in a consistent format, or even better the ISO 8601 standard format, there are some ways to read them in faster.
The first is the fastPOSIXct function in the fasttime package. It's one use is to read in dates formatted in the standard order: year, month, day, hour, minute, second. Because it's picky about the format it can be extremely fast.
3. fast_strptime()
Another option for fast parsing is the fast_strptime function in lubridate. It works a lot like parse_date_time, but instead of an order argument, it takes format argument. Like order, format is a charcter string that describes how the dates are formatted, but it is much less forgiving.
Compare the order argument and format arguments for parsing this date. In fast_strptime the components must be prefixed by a %, and you must explicitly include any separators (the dashes in this case). The component characters are also interpreted much more strictly. Where in parse_date_time, a lower case y will parse a year with or without the century, in fast_strptime you must specify the year here with a capital Y because it includes the century.
This kind of format string the same used by the base R functions strptime and strftime. Looking at their help is the best way to learn more about the format.
4. Exporting datetimes
We've talked a lot about importing datetimes but what if you want to export them, or print them in a nice way on a report.
If you are saving a dataset with datetimes for future use, it's helpful to know write_csv from the readr package will write datetime out in ISO 8601 format, which means they are readily read in again with read_csv.
5. Formatting datetimes
A slightly different problem is when you want to output a date in a nice way for human consumption, perhaps as a timestamp on a report. The easiest way to do this is using the stamp function in lubridate. You pass stamp an example of a date formatted the way you want it. For example, here I want the day of the week, followed by the month name, then numeric day of the month and year.
stamp returns a function, that you can then apply to any datetime object to format it this way. If you take a look at your returned function, you'll see all it does is pass the identified format string to the base R format function. If stamp doesn't pick the right format, use format, explicitly passing in a format argument, just like the one used by fast_strptime.
6. Let's practice!
Ok, time to practice reading in datetimes quickly and exporting datetimes.