**R-exercises**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Welcome to the third and last part of the “lubridate” exercises. If you missed Part 1 and 2 then please refer to the links below:

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:

` install.packages("lubridate") `

` library(lubridate) `

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 ` duration() `

function.

Functions related to durations start with a ** d ** (for example, `dseconds`

, `dminutes`

and `dyears`

).

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, `days`

, `minutes`

and `years`

).

## Rounding dates

Like numbers, date-times occur in order. This allows date-times to be rounded. lubridate

provides three methods that help perform this rounding: `round_date()`

, `floor_date()`

, and `ceiling_date()`

.

The first argument of each function is the date-times to be rounded. The second argument is the unit to round to.

**Exercise 1**

Create a ** duration ** object having 260 seconds

**Exercise 2**

Create a ** duration ** object having 260 minutes and divide it by 60

**Exercise 3**

Create a ** duration ** object of 1 day length and substract from it 260 duration minutes

**Exercise 4**

Create a ** duration ** object of 12 days, 5 hours and 10 minutes

**Exercise 5**

Create a sequence of periods, from zero to 10, by 2 weeks

**Exercise 6**

round date ‘2016-10-16’ by ‘month’ unit

**Exercise 7**

round date ‘2016-10-16’ down, by ‘month’ unit

**Exercise 8**

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 !!

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.