Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Date and Time in R

Dates and Times


• R has developed a special representation for dates and times.
• Dates are represented by the Date class and times are represented by
the POSIXct or the POSIXlt class.
• Dates are stored internally as the number of days since 1970-01-01
• times are stored internally as the number of seconds since 1970-01-
01.
Dates in R

• Dates are represented by the Date class and can be coerced from a
character string using the as.Date() function
• x<-as.Date("2020/07/23")
•x
• class(x)
Dates in R
> ## Coerce a 'Date' object from character
> x<-as.Date("2020/07/23")
>x
[1] "2020-07-23"
> class(x)
[1] "Date”

To see the internal representation of a Date object by using the unclass() function

> unclass(x)
[1] 18466
Times in R
• Times are represented by the POSIXct or the POSIXlt class.
• POSIXct is just a very large integer under the hood.
• It use a useful class when you want to store times in something like a data
frame.
• POSIXlt is a list underneath and it stores a bunch of other useful
information like
• the day of the week, day of the year, month, day of the month. This is useful
when you need that kind of information.
POSIXlt and POSIXct function

• Times can be coerced from a character string using the as.POSIXlt or


as.POSIXct function.
x<-Sys.time()
>x
[1] "2020-07-23 07:49:30 IST"
> class(x)
[1] "POSIXct" "POSIXt"
Printing the metadata
• The POSIXlt object contains some useful metadata.
#checking the metadata - using POSIXlt object
m<-as.POSIXlt(x)
m
#metadata using unclass
names(unclass(m))
> m<-as.POSIXlt(x)
>m
[1] "2020-07-23 07:49:30 IST"
> names(unclass(m))
[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday"
[9] "isdst" "zone" "gmtoff"
strptime()
• strptime() takes a character vector that has dates and times and
converts them into to a POSIXlt object.
> datestring<-"July 23, 2020 06:20"
> convertedForm<-strptime(datestring,"%B %d, %Y %H:%M")
> class(convertedForm)
[1] "POSIXlt" "POSIXt"
> convertedForm
[1] "2020-07-23 06:20:00 IST"
Operations on Dates and Times
• You can use mathematical operations on dates and times.
• + , -, ==, <=
Error

x <- as.Date("2020-02-01")
y <- strptime("10 Jan 2020 09:00:00", "%d %b %Y %H:%M:%S")
#y <- strptime(" Feb 5, 2020 11:34:21", "%b %d, %Y %H:%M:%S")
x-y

Error in x - y : non-numeric argument to binary operator


In addition: Warning message:
Incompatible methods ("-.Date", "-.POSIXt") for "-"
>
• #can't perform as the classes are different viz., Date and POSIXlt
class(x)
class(y)

> class(x)
[1] "Date"
> class(y)
[1] "POSIXlt" "POSIXt"
Additional Information
• Considers leap years, leap seconds, daylight savings, and time zones

x <- as.Date("2007-02-01")
y <- strptime("1 March 2007 09:00:00", "%d %b %Y %H:%M:%S")
x<-as.POSIXlt(x)
x-y

Time difference of -28.14583 days


Different time zones
> x <- as.POSIXct("2020-07-23 08:00:00")
>x
[1] "2020-07-23 08:00:00 IST"
> xgmt<-as.POSIXct("2020-07-23 08:00:00", tz="GMT")
> xgmt
[1] "2020-07-23 08:00:00 GMT"
>
> xgmt-x
Time difference of 5.5 hours
Data Transformation – References
• https://dplyr.tidyverse.org/reference/select.html
• https://dplyr.tidyverse.org/reference/filter.html
• ……
• arrange(), mutate(), rename(), summarise() …

You might also like