1. Extracting parts of a datetime
Once you've got dates in R, you'll often find yourself pulling them apart again. Why? Because exploring patterns in time often involves summarizing at different levels of resolution, like grouping data into months or averaging over hours.
2. Extracting parts of a datetime
In lubridate extracting components of a datetime is super easy, you just call the function with the name of the component you want. year pulls out the year, month the month and day the day of the month. There is also hour, minute and second to pull out the time components.
3. Extracting parts of a datetime
A few not so obvious components you can extract are: wday for the weekday and yday for the day of the year between 1 and 365 (or 366 in a leap year). You can also get the timezone component with tz.
4. Setting parts of a datetime
All these extractor functions can also be used to set a component, so, for example to take the date 2013/23/02 and change the year to 2017 we can use the year function on the left hand side of an assignment.
5. Other useful functions
There are a few other useful functions for accessing properties of a datetime: leap_year, am, pm and dst all return logical values, depending on whether the datetime is in a leap year, is in the morning, is in the afternoon, or daylight savings is in effect. quarter and semester return a numeric value identifying which quarter or half of the year a datetime occurs.
6. Let's practice!
Ok, time to practice and then see how useful these functions can be.