Cornell College
DSC 223 - Spring 2024 Block 7
R
provides several options for dealing with date and date/time data.R
, date and date/time objects have their own class too.R
, the built-in as.Date()
function with Date
class handles dates (without times); and the as.POSIXct()
function with POSIXct
class and as.POSIXlt()
function with POSIXlt
class allow for dates and times with control for time zones.Type | Function | Class | Description |
---|---|---|---|
date | as.Date() |
Date |
represent calendar dates. |
date-time | as.POSIXct() |
POSIXct |
stores seconds since epoch (since January 1, 1970 ). |
date-time | as.POSIXlt() |
POSIXlt |
stores a list of day, month, year, hour, minute, second, etc. |
Portable Operating System Interface
and it is a family of standards specified by the IEEE Computer Society
for maintaining compatibility between different operating systems.ct
stands for calendar time.January 1, 1970
as the epoch which is an origin (reference) day so that the dates can be converted to numbers. Dates older than the origin are stored as negative integers.R
, except for the POSIXlt
class, dates are stored internally as the number of days or seconds from January 1, 1970
.POSIXlt
class stores date/time values as a list of components (day, month, year, etc.) making it easy to extract these parts.POSIXlt
class, the POSIXct
class is the usual choice for storing dates in R
.YYYY-MM-DD
and uses the 24 hour clock system.R
is also the ISO 8601 standard
for date/time.as.Date()
function in base R
.[1] "numeric"
[1] "character"
[1] "Date"
[1] 18904
[1] "2021-11-01" "2021-10-01" "2021-09-01"
by=argument
to the seq()
function can be specified in any units of time that the function accepts, making it very easy to generate sequences of dates.seq(from=as.Date('2021-10-04'), to=as.Date('2022-01-01'), by='10 days') #1 day, 10 days, 2 weeks, 3 months
[1] "2021-10-04" "2021-10-14" "2021-10-24" "2021-11-03" "2021-11-13" "2021-11-23"
[7] "2021-12-03" "2021-12-13" "2021-12-23"
by
argument can be: “secs”, “mins”, “hours”, “days”, “weeks”, and “months”.as.Date()
function allows a variety of input formats through the format
input argument.Code | Value |
---|---|
%d | Day of the month (decimal number) |
%m | Month (decimal number) |
%b | Month (abbreviated) |
%B | Month (full name) |
%y | Year (2 digit) |
%Y | Year (4 digit) |
format()
function.Use:
Date
when there is no time component,POSIXct
when dealing with time and time zones, andPOSIXlt
when you want to access/extract the different components.tidyverse ecosystem
includes lubridate package for dealing with date-times and time-spans.lubridate
we have to load the package:lubridate
package has a number of functions to convert strings to date and date-time objects.YYYY-MM-DD HH:MM:SS
.lubridate
we can use a combination of the letters ‘d’, ‘m’, ‘y’ (standing for day, month, year).Function | Description |
---|---|
ymd() |
converts characters into YYYY-MM-DD format. |
ydm() |
converts characters into YYYY-MM-DD format. |
mdy() |
converts characters into YYYY-MM-DD format. |
dmy() |
converts characters into YYYY-MM-DD format. |
hms() |
converts characters into HH:MM:SS format. |
hm() |
converts characters into HH:MM format. |
h() |
converts characters into HH format. |
#if we thing that these strings are in year-month-day format,
#then put all of them in a standard year-month-day format.
x <- c(20090101, "2009-01-02", "2009 01 03", "2009-1-4",
"2009-1, 5", "Created on 2009 1 6", "200901 !!! 07")
ymd(x)
[1] "2009-01-01" "2009-01-02" "2009-01-03" "2009-01-04" "2009-01-05" "2009-01-06"
[7] "2009-01-07"
[1] "2006-03-19 20:30:10 UTC"
[1] "2006-03-19 20:30:10 UTC"
[1] "2006-03-19 20:30:10 UTC"
[1] "2006-03-19 20:30:10 UTC"
data-time
objects.Function | Description |
---|---|
date() |
Date component |
year() |
Year component |
month(, label) |
Month component |
day() |
Day of month |
week() |
Week of the year |
wday(, label) |
Day of week |
yday() |
Day of year |
hour() |
Hour |
minute() |
Minute |
second() |
Second |
[1] 2021
[1] 3
[1] 10
[1] 2
[1] Mon
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
[1] 67
lubridate
also has a function to extract hours, minutes and seconds: