install.packages("lubridate")Date and Time
Requirements
For this session it is recommended to install the package lubridate.
For the exercises, you will need to download the data set fuel-prices into your working directory. Details on the content of the data are here.
download.file(
url = "https://kamapu.gitlab.io/coachingR/Topics/Data/fuel-prices.csv",
destfile = "fuel-prices.csv", method = "curl")Date in R
The date is represented in R by the class Date. Internally, this class stores the number of days since 1 January 1970. This allows us to record events and apply mathematics to them.
# The refeence date
as.Date(0, origin = "1970-01-01")[1] "1970-01-01"
# Today's date
as.Date(Sys.Date())[1] "2025-12-10"
# Days since them
difftime(as.Date(Sys.Date()), as.Date(0, origin = "1970-01-01"))Time difference of 20432 days
Text to Date and Vice Versa
The usual way of representing dates as text by R is in the format “YYYY-MM-DD”, for example 1 January 2000.
new_world <- as.Date("2000-01-01")
class(new_world)[1] "Date"
new_world[1] "2000-01-01"
# Days since the change of Millenia
difftime(as.Date(Sys.Date()), new_world,)Time difference of 9475 days
To convert dates and times from and to characters in R, there is a list of tags starting with the symbol “%”. The full list of tags can be found in the help for the strptime() function:
# See the help documentation
?strptimeThe usual date format is represented by the string “%Y-%m-%d”.
We can use the strptime() function to convert strings to Date, declaring the appropriate formats.
# Approximate Beethoven's birthday
as.Date(strptime("17.12.1770", format = "%d.%m.%Y"))[1] "1770-12-17"
# Einstein's birthday (system must be set in English language)
as.Date(strptime("14 March 1879", format = "%d %B %Y"))[1] "1879-03-14"
To extract elements from a date vector, you can use the function format() for it. Note that the outputs will be always a character value, thus you may need to coerce numbers to integers.
# Hypothetical birthday
bd <- "15-06"
# Birthdays along 10 years
bd <- paste(bd, c(2025:2035), sep = "-")
# Transform to Date
bd <- as.Date(strptime(bd, format = "%d-%m-%Y"))
# Weekdays
next_birthdays <- data.frame(
date = bd,
weekday = format(bd, format = "%A"))
next_birthdays date weekday
1 2025-06-15 Sunday
2 2026-06-15 Monday
3 2027-06-15 Tuesday
4 2028-06-15 Thursday
5 2029-06-15 Friday
6 2030-06-15 Saturday
7 2031-06-15 Sunday
8 2032-06-15 Tuesday
9 2033-06-15 Wednesday
10 2034-06-15 Thursday
11 2035-06-15 Friday
The use of names for dates depends on the language set in your system. You may be able to change this setting on the fly.
# Save the current locale
locale <- Sys.getlocale("LC_TIME")
# Weekdays in German
Sys.setlocale("LC_TIME", "de_DE.UTF-8")Warning in Sys.setlocale("LC_TIME", "de_DE.UTF-8"): OS reports request to set
locale to "de_DE.UTF-8" cannot be honored
[1] ""
format(bd, format = "%A") [1] "Sunday" "Monday" "Tuesday" "Thursday" "Friday" "Saturday"
[7] "Sunday" "Tuesday" "Wednesday" "Thursday" "Friday"
# Weekdays in Spanish
Sys.setlocale("LC_TIME", "es_ES.utf8")Warning in Sys.setlocale("LC_TIME", "es_ES.utf8"): OS reports request to set
locale to "es_ES.utf8" cannot be honored
[1] ""
format(bd, format = "%A") [1] "Sunday" "Monday" "Tuesday" "Thursday" "Friday" "Saturday"
[7] "Sunday" "Tuesday" "Wednesday" "Thursday" "Friday"
# Back to old locale
Sys.setlocale("LC_TIME", locale)[1] "C"
format(bd, format = "%A") [1] "Sunday" "Monday" "Tuesday" "Thursday" "Friday" "Saturday"
[7] "Sunday" "Tuesday" "Wednesday" "Thursday" "Friday"
Time in R
Time in R is handled by two alternative classes, namely POSIXct and POSIXlt. For example, looking at the current time.
Sys.time()[1] "2025-12-10 23:53:55 UTC"
Here we recognise the date as part of the information, including the time (hours, minutes and seconds) and the time zone.
Here you can use again the well known tags and the function strptime() to convert characters to time.
just_now <- strptime("06.05.2024 um 17.30",
format = "%d.%m.%Y um %H.%M", tz = "Europe/Berlin")
just_now[1] "2024-05-06 17:30:00 CEST"
Time conversion
May we like to convert the time to a different time zone, we can use the package lubridate.
library(lubridate)
Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
# Santiago de Chile
with_tz(just_now, tz = "America/Santiago")[1] "2024-05-06 11:30:00 -04"
# Kenya
with_tz(just_now, tz = "Africa/Nairobi")[1] "2024-05-06 18:30:00 EAT"
A complete list of time zones can be found at this Wikipedia page (column TZ identifier).
- Read the data set fuel-prices.csv. Format the date column in R accordingly.
- In which weekday is the summary done?
- Do a boxplot comparing the prices between years.