What can you extract?
As you saw in the video, components of a datetime can be extracted by lubridate
functions with the same name like year()
, month()
, day()
, hour()
, minute()
and second()
. They all work the same way just pass in a datetime or vector of datetimes.
There are also a few useful functions that return other aspects of a datetime like if it occurs in the morning am()
, during daylight savings dst()
, in a leap_year()
, or which quarter()
or semester()
it occurs in.
Try them out by exploring the release times of R versions using the data from Chapter 1.
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
We've put release_time
, the datetime
column of the releases
dataset from Chapter 1, in your workspace.
- Examine the
head()
ofrelease_time
to verify this is a vector of datetimes. - Extract the month from
release_time
and examine the first few withhead()
. - To see which months have most releases, extract the month then pipe to
table()
. - Repeat, to see which years have the most releases.
- Do releases happen in the morning (UTC)? Find out if the hour of a release is less than
12
and summarise withmean()
. - Alternatively use
am()
to find out how often releases happen in the morning.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Examine the head() of release_time
head(___)
# Examine the head() of the months of release_time
head(___(release_time))
# Extract the month of releases
___(release_time) %>% table()
# Extract the year of releases
___(release_time) %>% table()
# How often is the hour before 12 (noon)?
mean(___(release_time) < ___)
# How often is the release in am?
mean(___(release_time))