lubridate package recogniced the common separators used when recording dates (“-“, “/”, “.”, and “”).
Create Dates by Merging Data:
yr <- c("2012", "2013", "2014", "2015")
mo <- c("1", "5", "7", "2")
day <- c("02", "22", "15", "28")
# ISOdate converts to a POSIXct object
ISOdate(year = yr, month = mo, day = day)
## [1] "2012-01-02 12:00:00 GMT" "2013-05-22 12:00:00 GMT"
## [3] "2014-07-15 12:00:00 GMT" "2015-02-28 12:00:00 GMT"
# truncate the unused time data by converting with as.Date
as.Date(ISOdate(year = yr, month = mo, day = day))
#Extract & manipulate parts of dates
Manipulating data with lubridate
library(lubridate)
x <- c("2015-07-01", "2015-08-01", "2015-09-01")
year(x)
## [1] 2015 2015 2015
# default is numerical value
month(x)
## [1] 7 8 9
# show abbreviated name
month(x, label = TRUE)
## [1] Jul Aug Sep
## 12 Levels:
#Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
# show unabbreviated name
month(x, label = TRUE, abbr = FALSE)
## [1] July August September
## 12 Levels:
#January < February < March < April < May < June < ... < December
wday(x, label = TRUE, abbr = FALSE)
## [1] Wednesday Saturday Tuesday
## 7 Levels:
#Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday
Change the values of date elements
# convert to date format
x <- ymd(x)
x
## [1] "2015-07-01 UTC" "2015-08-01 UTC" "2015-09-01 UTC"
# change the days for the dates
mday(x)
## [1] 1 1 1
mday(x) <- c(3, 10, 22)
x
## [1] "2015-07-03 UTC" "2015-08-10 UTC" "2015-09-22 UTC"
# can also use 'update()' function
update(x, year = c(2013, 2014, 2015), month = 9)
## [1] "2013-09-03 UTC" "2014-09-10 UTC" "2015-09-22 UTC"
# can also add/subtract units
x + years(1) - days(c(2, 9, 21))
## [1] "2016-07-01 UTC" "2016-08-01 UTC" "2016-09-01 UTC"
Creating sequences with time is very similar; however, we need to make sure our date object is POSIXct rather than just a Date object (as produced by as.Date):
R stores date and time objects as numbers. But we can calculated it such as logical comparisons, addition, subtraction, and working with durations.
x <- Sys.Date()
x
## [1] "2015-09-26"
y <- as.Date("2015-09-11")
x > y
## [1] TRUE
x - y
## Time difference of 15 days
# last leap year
x <- as.Date("2012-03-1")
y <- as.Date("2012-02-28")
x - y
## Time difference of 2 days
# example with time zones
x <- as.POSIXct("2015-09-22 01:00:00", tz = "US/Eastern")
y <- as.POSIXct("2015-09-22 01:00:00", tz = "US/Pacific")
y == x
## [1] FALSE
y - x
## Time difference of 3 hours
With the lubridate package
library(lubridate)
x <- now()
x
## [1] "2015-09-26 10:08:18 EDT"
y <- ymd("2015-09-11")
x > y
## [1] TRUE
x - y
## Time difference of 15.5891 days
y + days(4)
## [1] "2015-09-15 UTC"
x - hours(4)
## [1] "2015-09-26 06:08:18 EDT"
# create new duration (represented in seconds)
new_duration(60)
## [1] "60s"
# create durations for minutes, hours, years
dminutes(1)
## [1] "60s"
dhours(1)
## [1] "3600s (~1 hours)"
dyears(1)
## [1] "31536000s (~365 days)"
# add/subtract durations from date/time object
x <- ymd_hms("2015-09-22 12:00:00")
x + dhours(10)
## [1] "2015-09-22 22:00:00 UTC"
x + dhours(10) + dminutes(33) + dseconds(54)
## [1] "2015-09-22 22:33:54 UTC"
#Dealing with time zones & daylight savings
Change the time zone for a date/time with the with_tz() function.
library(lubridate)
time <- now()
time
## [1] "2015-09-26 10:30:32 EDT"
with_tz(time, tzone = "MST")
## [1] "2015-09-26 07:30:32 MST"
Change the time zone without changing the clock time with force_tz().
Savings times to eliminate impacts on date/time calculations.
# most recent daylight savings time
ds <- ymd_hms("2015-03-08 01:59:59", tz = "US/Eastern")
# if we add a duration of 1 sec we gain an extra hour
ds + dseconds(1)
## [1] "2015-03-08 03:00:00 EDT"
# add a duration of 2 hours will reflect actual daylight
#savings clock time
# that occured 2 hours after 01:59:59 on 2015-03-08
ds + dhours(2)
## [1] "2015-03-08 04:59:59 EDT"
# add a period of two hours will reflect clock time that normally
#occurs after
# 01:59:59 and is not influenced by daylight savings time.
ds + hours(2)
## [1] "2015-03-08 03:59:59 EDT"