1 Goal


The goal of this tutorial is to read dates in the POSIXct format. In addition we will see the power of defining time zones.


2 Read the Data


# First we load the libraries
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
# In this tutorial we are going to use Date and time data
Time_table <- read.csv("timetable.csv", stringsAsFactors = FALSE)
str(Time_table)
## 'data.frame':    34584 obs. of  2 variables:
##  $ Date: chr  "16/12/06" "16/12/06" "16/12/06" "16/12/06" ...
##  $ Time: chr  "18:00:00" "19:00:00" "20:00:00" "21:00:00" ...
# First we are going to create a new column containing the full date_time
Time_table <- mutate(Time_table, DateTime = paste(Date, Time))
str(Time_table)
## 'data.frame':    34584 obs. of  3 variables:
##  $ Date    : chr  "16/12/06" "16/12/06" "16/12/06" "16/12/06" ...
##  $ Time    : chr  "18:00:00" "19:00:00" "20:00:00" "21:00:00" ...
##  $ DateTime: chr  "16/12/06 18:00:00" "16/12/06 19:00:00" "16/12/06 20:00:00" "16/12/06 21:00:00" ...
# Now we transform from text to date format variable
Time_table$DateTime <- strptime(Time_table$DateTime, "%d/%m/%y %H:%M:%S")
str(Time_table$DateTime)
##  POSIXlt[1:34584], format: "2006-12-16 18:00:00" "2006-12-16 19:00:00" ...

3 Defining the time zone

3.1 List of time zones


# We can find which is our timezone using the Sys.timezone function
# Interesting timezones are GMT where there is no daylight saving time
Sys.timezone()
## [1] "Europe/Madrid"
# There is a list of all time zones stored in R 
# The function OlsonNames gives all the possible time zones
# We can use this timezones to change the hour later
head(OlsonNames(), 20)
##  [1] "Africa/Abidjan"       "Africa/Accra"         "Africa/Addis_Ababa"  
##  [4] "Africa/Algiers"       "Africa/Asmara"        "Africa/Asmera"       
##  [7] "Africa/Bamako"        "Africa/Bangui"        "Africa/Banjul"       
## [10] "Africa/Bissau"        "Africa/Blantyre"      "Africa/Brazzaville"  
## [13] "Africa/Bujumbura"     "Africa/Cairo"         "Africa/Casablanca"   
## [16] "Africa/Ceuta"         "Africa/Conakry"       "Africa/Dakar"        
## [19] "Africa/Dar_es_Salaam" "Africa/Djibouti"

3.2 Changing the time zone


# The time zone allows us to compare times in different parts of the world 
# We can define the time zone of Madrid for example
Time_table$DateTime <- as.POSIXct(Time_table$DateTime, tz = "Europe/Madrid")
summary(Time_table$DateTime)
##                  Min.               1st Qu.                Median 
## "2006-12-16 18:00:00" "2007-12-12 00:45:00" "2008-12-06 07:30:00" 
##                  Mean               3rd Qu.                  Max. 
## "2008-12-06 07:06:15" "2009-12-01 14:15:00" "2010-11-26 21:00:00"
# We can change now to a different time zone and check the different time 
# We can use the function with_tz for that purpose
summary(with_tz(Time_table$DateTime, "US/Central"))
##                  Min.               1st Qu.                Median 
## "2006-12-16 11:00:00" "2007-12-11 17:45:00" "2008-12-06 00:30:00" 
##                  Mean               3rd Qu.                  Max. 
## "2008-12-06 00:06:15" "2009-12-01 07:15:00" "2010-11-26 14:00:00"

4 Conclusion


In this tutorial we have learnt how to read dates and times and format them in the best way. In addition we have learnt how to define the proper time zone and avoid problems like daylight saving time.