Module 40 Working with dates & times
- Be able to read dates, and convert objects to dates
- Be able to convert dates, extract useful information, and modify them
- Use date times
- Gain familiarity with the lubridate package
Hadley Wickham’s tutorial on dates starts with 3 simple questions:
- Does every year have 365 days?
- Does every day have 24 hours?
- Does every minute have 60 seconds?
"I’m sure you know that not every year has 365 days, but do you know the full rule for determining if a year is a leap year? (It has three parts.)
You might have remembered that many parts of the world use daylight savings time (DST), so that some days have 23 hours, and others have 25.
You might not have known that some minutes have 61 seconds because every now and then leap seconds are added because the Earth’s rotation is gradually slowing down.
Dates and times are hard because they have to reconcile two physical phenomena (the rotation of the Earth and its orbit around the sun) with a whole raft of geopolitical phenomena including months, time zones, and DST.
This chapter won’t teach you every last detail about dates and times, but it will give you a solid grounding of practical skills that will help you with common data analysis challenges."
First, install the
Getting familiar with the
Get today’s date:
This looks like a simple character string, but it is not. There are all sorts of date-time calculations in the background.
To demonstrate this, let’s bring in a simple string:
Note that class type impacts what you can do with text. The following causes an error…
… but this does not:
When you are working with a
datetime object, you can add and subtract time to it.
Add or substract seconds:
Add or subtract hours:
Simplify to just the date:
See how time flies:
Converting to dates from strings
lubridate package was built to handle dates of various input formats. The following functions convert a character with a particular format into a standard
This also works if the single-digits dates are not padded with a
Other formats can also be handled:
Extracting components from dates
Let’s practice extracting information from the following
Get the month:
Get the day of month:
Get the day of year:
Get the day of week:
Get the name of the day of week:
Get the hour of the day:
Get the minute of the hour:
Get the seconds of the minute:
Dealing with time zones
When working with dates and times in
R, time zones can be a major pain, but the
lubridate package tries to make this simpler.
Adjust timezones for dates:
Adjust time zones for date-times:
Don’t know what time zone your computer is working in? Use this function:
To get a list of time zones accepted in
R, use the function
OlsonNames() (there are about 500 options):
 "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa"  "Africa/Algiers" "Africa/Asmara" "Africa/Asmera"  "Africa/Bamako" "Africa/Bangui" "Africa/Banjul"  "Africa/Bissau" "Africa/Blantyre" "Africa/Brazzaville"  "Africa/Bujumbura" "Africa/Cairo" "Africa/Casablanca"  "Africa/Ceuta" "Africa/Conakry" "Africa/Dakar"  "Africa/Dar_es_Salaam" "Africa/Djibouti" "Africa/Douala"  "Africa/El_Aaiun" "Africa/Freetown" "Africa/Gaborone"  "Africa/Harare" "Africa/Johannesburg" "Africa/Juba"  "Africa/Kampala" "Africa/Khartoum" "Africa/Kigali"  "Africa/Kinshasa" "Africa/Lagos" "Africa/Libreville"  "Africa/Lome" "Africa/Luanda" "Africa/Lubumbashi"  "Africa/Lusaka" "Africa/Malabo" "Africa/Maputo"  "Africa/Maseru" "Africa/Mbabane" "Africa/Mogadishu"  "Africa/Monrovia" "Africa/Nairobi" "Africa/Ndjamena"  "Africa/Niamey" "Africa/Nouakchott" "Africa/Ouagadougou"  "Africa/Porto-Novo" "Africa/Sao_Tome"
At some point you may have reason to force the timezone of a
datetime object to change without actually changing the date or time. To do so, use the function
Using timestamps instead
One way to avoid timezone issues is to convert a
datetime object to a numeric timestamp.
Timesetamps record the number of seconds that have passed since midnight GMT on January 1, 1970. It doesn’t matter which timezone you are standing in; the seconds that have passed since that moment will be the same:
Timestamps can simplify things when you are doing a lot of adding and substracting with time. Timestamps are just seconds; they are just numbers. So they are much less of a black box than
You can always convert from a timestamp back into a
Use the appropriate
lubridate function to parse each of the following dates:
January 1, 2010
c('August 19 (2015)', 'July 1 (2015)')
Work with this vector of dates:
6. Create a dataframe that has the following columns:
raw(containing the original string)
dom(day of month)
doy(day of year)
7. Now add two more variables:
diff(the difference, in days, between this time and midnight GMT on January 1, 1970)
Record of a child’s cough
First, download the data:
8. Create a
dow (day of week) column.
9. Create a
date (without time) column.
10. How many coughs happened each day?
11. Create a chart of coughs by day.
11. Look up
floor_date. Use it to get the number of coughs by date-hour.
12. Create an
13. Use the
hour variable to create a
night_day column indicating whether the cough was occurring at night or day.
14. Does this child cough more at night or day?