Warning: Parameter 2 to wp_hide_post_Public::query_posts_join() expected to be a reference, value given in /home/rexercis/public_html/wp-includes/class-wp-hook.php on line 286
In this part, I’ll cover the following topics:
1. Durations (exact spans of time)
2. Periods (relative spans of time)
3. Rounding dates
As always, in case you skipped the previous parts, let’s start by downloading and installing “lubridate” package:
Answers to the exercises are available here.
If you have different solutions, feel free to post them.
Durations and Periods
“lubridate” package has four types of “date/time” objects (Can be remembered by the acronym “IIDP”) :
1. Instant: A specific moment in time (this concept is explained in the previous part).
2. Interval: Based on “start” instant and “end” instant we create an “interval” (this concept is also explained in the previous part).
3. Duration: Following the concept of “interval”, we can “remove” the start and end points and what will remain is an exact time period, which can be measured in seconds.
Thus, durations are fixed time periods, not influenced by leap years or other time changes.
Durations are recommended when comparing time-based objects, such as speeds, execution times, etc.
A duration object is created, for example, using the
Functions related to durations start with a d (for example,
4. Period: Time spans measured in units larger than seconds (and thus taking into consideration leap years, daylight saving time and similar “time-related” issues), are called periods.
Thus, periods are relative time periods, influenced by leap years or other time changes.
Periods are used usually when calculating differences in calendar dates.
Functions related to durations start without a d (for example,
Like numbers, date-times occur in order. This allows date-times to be rounded. lubridate
provides three methods that help perform this rounding:
The first argument of each function is the date-times to be rounded. The second argument is the unit to round to.
Create a duration object having 260 seconds
Create a duration object having 260 minutes and divide it by 60
Create a duration object of 1 day length and substract from it 260 duration minutes
Create a duration object of 12 days, 5 hours and 10 minutes
Create a sequence of periods, from zero to 10, by 2 weeks
round date ‘2016-10-16’ by ‘month’ unit
round date ‘2016-10-16’ down, by ‘month’ unit
round date ‘2016-10-16’ up, by ‘month’ unit
Exercise 9 and 10
Calculating Thanksgiving Day in 2016:
Thanksgiving is celebrated on the fourth Thursday of November.
So we will execute the following steps:
1. Create a date/time object (date1 variable) representing Novevember 1st, 2016
2. Check which day of the week November 1st is.
3. Add the needed days to reach the upcoming Thursday (date2 variable)
4. Make sure your calculation is correct (e.g. you reached the upcoming Thursday)
5. Add 3 weeks
Congratulations and happy Thanksgiving !!
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below