Examining intervals. Reigns of kings and queens
You can create an interval by using the operator %--% with two datetimes. For example ymd("2001-01-01") %--% ymd("2001-12-31") creates an interval for the year of 2001.
Once you have an interval you can find out certain properties like its start, end and length with int_start(), int_end() and int_length() respectively.
Practice by exploring the reigns of kings and queens of Britain (and its historical dominions).
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
We've put the data monarchs in your workspace.
- Print
monarchsto take a look at the data - Create a new column called
reignthat is an interval betweenfromandto. - Create another new column,
length, that is the interval length ofreign. The rest of the pipeline we've filled in for you, it arranges by decreasinglengthand selects thename,lengthanddominioncolumns.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print monarchs
# Create an interval for reign
monarchs <- monarchs %>%
mutate(___ = ___ %--% ___)
# Find the length of reign, and arrange
monarchs %>%
mutate(length = ___) %>%
arrange(desc(length)) %>%
select(name, length, dominion)