Base R has functions to get the current date and time.
Also the lubridate package offers fast and user friendly parsing of date-time data.
Sys.Date()
## [1] "2020-06-15"
Sys.time()
## [1] "2020-06-15 01:09:19 AEST"
# install.packages("lubridate")
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
now()
## [1] "2020-06-15 01:09:19 AEST"
If dates are loaded as factors, characters or integers, we need to convert them to dates.
# as.date()
The default date format is YYYY-MM-DD;
x <- c("08/03/2018", "23/03/2016", "30/01/2018")
y <- c("08.03.2018", "23.03.2016", "30.01.2018")
This time the string format is DD/MM/YYYY for x and DD.MM.YYYY for y. We need to account for this below.
x_date <- as.Date(x, format = "%d/%m/%Y")
y_date <- as.Date(y, format = "%d.%m.%Y")
The lubridate package can automatically recognise common separators used when recording dates.
As a result, you only need to focus on specifying the order of the date elements.
a <- c("26/03/2016", "08/12/2011", "05/01/2013")
In order to convert ‘a’ into a date, we can use dmy() (day, month, year)
library(lubridate)
a_date <- dmy(a)
a_date
## [1] "2016-03-26" "2011-12-08" "2013-01-05"
task1 <- c("08032018", "29062017", "23032016", "30012018")
dmy(task1)
## [1] "2018-03-08" "2017-06-29" "2016-03-23" "2018-01-30"
Sometimes, instead of a single string, we will have date-time spread across multiple columns.
To create a date/time from this sort of input, we can use make_date() for dates and make_datetime() for date-times.
# flights %>% mutate(departure = make_datetime(year, month, day, hour, minute))
The code above brings together those columns and gives us combined date time data.
We can extract individual parts of the date with the accessor functions in lubridate.
Often we may require to compute a new variable from the date-time information.
In order to calculate time difference, simply subtract the two dates.
my_age <- today() - ymd(19810529)
my_age
## Time difference of 14262 days
OR, use the difftime() function.
your_age <- difftime(today(), ymd(19810529))
your_age
## Time difference of 14262 days
To chang the units in time differences, use units argument.
my_age_secs<-difftime(today(), ymd(19810529), units = "secs")
my_age_secs
## Time difference of 1232236800 secs
Units can be:
Note that difftime objects do not accept “years” as a value for units.
To get my age in years:
my_age_days<-difftime(today(), ymd(19810529), units = "days")
my_age_years<- as.numeric(my_age_days/365.25)
my_age_years
## [1] 39.04723
A sequence of dates/times can be created using seq() function.
Create a sequence of years from 1980 to 2018 by 2.
even_years <- seq(from = 1980, to=2018, by = 2)
even_years
## [1] 1980 1982 1984 1986 1988 1990 1992 1994 1996 1998 2000 2002 2004 2006 2008
## [16] 2010 2012 2014 2016 2018
Create a sequence of hours from 1/1/2018 9:00 to 1/1/2018 12:00.
hour_list <- seq (from=ymd_hm("2018-1-1 9:00"), to=ymd_hm("2018-1-1 12:00"), by = "hour")
hour_list
## [1] "2018-01-01 09:00:00 UTC" "2018-01-01 10:00:00 UTC"
## [3] "2018-01-01 11:00:00 UTC" "2018-01-01 12:00:00 UTC"
A difftime class object records a time span of seconds, minutes, hours, days or weeks.
lubridate provides an alternative which always uses seconds: the duration.
today() + dweeks(3)
## [1] "2020-07-06"
today() + ddays(1)
## [1] "2020-06-16"
today() - dyears(1)
## [1] "2019-06-15 18:00:00 UTC"
today() - ( dyears(2) + dweeks(12) + dhours(15) )
## [1] "2018-03-22 21:00:00 UTC"