R Programming Date and Time Tutorial

R Programming Date and Time Tutorial

Created By: Debasis Das (4-Sep-2016)

In this tutorial we will see how date and time works in R Programming and we will create a lot of samples

Date & Time in R

  • In R Programming Dates are represented by the Date Class and Times are represented by the POSIXct and POSIXlt classes.
  • Dates are stored internally as the number of days since 1970-01-01
  • In POSIXct Times are stored internally as the number of seconds since 1970-01-01
  • In POSIXlt times are stored as list of seconds, minutes, hours etc

Date and Time in R Programming

Sys.date()

lets get started with an example
> date1 <- Sys.Date()
> date1
[1] “2016-09-04″
> class(date1)
[1] “Date”
> unclass(date1)
[1] 17048
when we unclassed date1 we saw the number of days passed since 1970-01-01

as.Date()

We can also create dates using as.Date() function
> date2 <- as.Date(“2016-09-09″)
> date2
[1] “2016-09-09″
> class(date2)
[1] “Date”
> unclass(date2)
[1] 17053

For creating dates prior to 1970-01-01
> date3<-as.Date(“1967-01-01″)
> date3
[1] “1967-01-01″
> class(date3)
[1] “Date”
> unclass(date3)
[1] -1096
Here the date is stored as a negative number as the date is earlier than 1970-01-01


> weekdays(date1)
[1] “Sunday”
> format(date1,”%d %b %Y”)
[1] “04 Sep 2016″
> date4<-as.Date(“04/21/2016″, format=”%m/%d/%Y”)
> date4
[1] “2016-04-21″
> date5<-as.Date(“October 10,2016″,format = “%B %d,%Y”)
> date5
[1] “2016-10-10″

> weekdays(date5)
[1] “Monday”
> months(date5)
[1] “October”
> weekdays(time5)
[1] “Sunday”
> months(time5)
[1] “September”
> quarters(time5)
[1] “Q3″
> julian(time5)
Time difference of 17048.63 days
> julian(time5,origin = as.POSIXct(“2000-01-01″, tz = “GMT”))
Time difference of 6091.626 days


Formatting the date

The following symbols can be used with the format() function to print dates

%d – day as a number (0-31)

%a – abbreviated weekday (Mon)

%A – Unabbreviated weekday (Monday)

%m – month (00-12)

%b – abbreviated month – Jan

%B – unabbreviated month (January)

%y – 2 digit year (16)

%Y – 4 digit year (2016)

> today<-Sys.Date()
> today
[1] “2016-09-04″
> format(today, format=”%d-%m-%y”)
[1] “04-09-16″
> format(today, format=”%d-%m-%Y”)
[1] “04-09-2016″
> format(today, format=”%d-%b-%Y”)
[1] “04-Sep-2016″
> format(today, format=”%d-%B-%Y”)
[1] “04-September-2016″
> format(today, format=”%a %d-%B-%Y”)
[1] “Sun 04-September-2016″
> format(today, format=”%A %d-%B-%Y”)
[1] “Sunday 04-September-2016″


 

Time difference using difftime

> date4-date5
Time difference of -172 days
> date5-date4
Time difference of 172 days

> difftime(date5, date4, units = “weeks”)
Time difference of 24.57143 weeks
> difftime(date5, date4, units = “hours”)
Time difference of 4128 hours
> difftime(date5, date4, units = “secs”)
Time difference of 14860800 secs
> difftime(date5, date4, units = “days”)
Time difference of 172 days
> difftime(date5, date4, units = “mins”)
Time difference of 247680 mins
> difftime(date5, date4, units = “auto”)
Time difference of 172 days
> difftime(date5, date4) #units not specified here, so default should be used
Time difference of 172 days

> dateVec<-as.Date(c(“2016-01-01″,”2017-01-01″,”2019-01-01″))
> dateVec
[1] “2016-01-01″ “2017-01-01″ “2019-01-01″
> diff(dateVec)
Time differences in days
[1] 366 730


Creating a sequence of dates

> date5
[1] “2016-10-10″
> dateSequence<-seq(date5, length=10, by = “day”)
> dateSequence
[1] “2016-10-10″ “2016-10-11″ “2016-10-12″ “2016-10-13″ “2016-10-14″ “2016-10-15″ “2016-10-16″ “2016-10-17″ “2016-10-18″ “2016-10-19″
> dateSequence1<-seq(date5, length=10, by = “week”)
> dateSequence1
[1] “2016-10-10″ “2016-10-17″ “2016-10-24″ “2016-10-31″ “2016-11-07″ “2016-11-14″ “2016-11-21″ “2016-11-28″ “2016-12-05″ “2016-12-12″
> dateSequence2<-seq(date5, length=10, by = 2) #skipping one day, alternate day
> dateSequence2
[1] “2016-10-10″ “2016-10-12″ “2016-10-14″ “2016-10-16″ “2016-10-18″ “2016-10-20″ “2016-10-22″ “2016-10-24″ “2016-10-26″ “2016-10-28″


 

POSIXCt

> time1<-as.POSIXct(“2016-09-05 22:32:12″)
> time1
[1] “2016-09-05 22:32:12 IST”

> time2 <- as.POSIXct(“2016-09-06 18:32:07″, format = “%Y-%m-%d %H:%M:%S”)
> time2
[1] “2016-09-06 18:32:07 IST”

> time2-time1
Time difference of 19.99861 hours

> time2>time1
[1] TRUE

> time3<-time2+24
> time3
[1] “2016-09-06 18:32:31 IST”
> time2
[1] “2016-09-06 18:32:07 IST”
> time3-time2
Time difference of 24 secs

> unclass(time3)
[1] 1473166951


 

POSIXlt

> time4<-as.POSIXlt(“2016-09-04 23:56:23″)
> time4
[1] “2016-09-04 23:56:23 IST”
> unclass(time4)
$sec
[1] 23

$min
[1] 56

$hour
[1] 23

$mday
[1] 4

$mon
[1] 8

$year
[1] 116

$wday
[1] 0

$yday
[1] 247

$isdst
[1] 0

$zone
[1] “IST”

$gmtoff
[1] NA

> unlist(time4)
sec min hour mday mon year wday yday isdst zone gmtoff
“23” “56” “23” “4” “8” “116” “0” “247” “0” “IST” NA

> time4$sec
[1] 23
> time4$mday
[1] 4
> time4$zone
[1] “IST”

> str(unclass(time4))
List of 11
$ sec : num 23
$ min : int 56
$ hour : int 23
$ mday : int 4
$ mon : int 8
$ year : int 116
$ wday : int 0
$ yday : int 247
$ isdst : int 0
$ zone : chr “IST”
$ gmtoff: int NA

> time5<-as.POSIXlt(Sys.time())
> time5
[1] “2016-09-04 20:31:23 IST”
> class(time5)
[1] “POSIXlt” “POSIXt”

> str(unclass(time5))
List of 11
$ sec : num 23.9
$ min : int 31
$ hour : int 20
$ mday : int 4
$ mon : int 8
$ year : int 116
$ wday : int 0
$ yday : int 247
$ isdst : int 0
$ zone : chr “IST”
$ gmtoff: int 19800
– attr(*, “tzone”)= chr [1:3] “” “IST” “IST”

> time6<-“October 17, 1986 08:24″
> time6Created<-strptime(time6, “%B %d, %Y %H:%M”)
> time6Created
[1] “1986-10-17 08:24:00 IST”
> class(time6Created)
[1] “POSIXlt” “POSIXt”

Posted in R Programming Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news