Selecting the right parsing function
lubridate provides a set of functions for parsing dates of a known order. For example, ymd() will parse dates with year first, followed by month and then day. The parsing is flexible, for example, it will parse the m whether it is numeric (e.g. 9 or 09), a full month name (e.g. September), or an abbreviated month name (e.g. Sep).
All the functions with y, m and d in any order exist. If your dates have times as well, you can use the functions that start with ymd, dmy, mdy or ydm and are followed by any of _h, _hm or _hms.
To see all the functions available look at ymd() for dates and ymd_hms() for datetimes.
Here are some challenges. In each case we've provided a date, your job is to choose the correct function to parse it.
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
For each date the ISO 8601 format is displayed as a comment after it, to help you check your work
- Choose the correct function to parse
x. - Choose the correct function to parse
y. - Choose the correct function to parse
z.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
library(lubridate)
# Parse x
x <- "2010 September 20th" # 2010-09-20
___(x)
# Parse y
y <- "02.01.2010" # 2010-01-02
___(y)
# Parse z
z <- "Sep, 12th 2010 14:00" # 2010-09-12T14:00
___(z)