LOADING

Type to search

Working with Time and Date variables in R Programming Language

To Know more about the Different Corporate Training & Consulting Visit our website www.Instrovate.com Or Email : info@instrovate.com or WhatsApp / Call at +91 74289 52788

Data Analytics R Programming

Working with Time and Date variables in R Programming Language

Share

Time

Time  and Date variables

There are a variety of different types specific to time data fields in R. Here we only look at two, the POSIXct and POSIXlt data types:

POSIXct

The POSIXct data type shows the number of seconds since the start of January 1 , 1970. If the date is before January 1, 1970 than it shows in negative numbers.

POSIXlT

The POSIXlt data type is a vector  and it shows following values:

1. Seconds

2. Minutes

3. Hours

4.Day of the month(1-31)

5. Month of the year(0-11)

6. Years since 1900

7. Day of the week(0-6 where 0 represents Sunday)

8. Day of the year(0-365)

9. Daylight savings time indicator  

We first check the various parameters of POSIXct and POSIXlt by taking help()  

help(“DateTimeClasses”)

It will open Help window to show DateTimeClasses.

Working with Time  and Date variables in R Programming Language 29

We can check today’s date by using Sys.Date() function.

today <- Sys.Date()

today

Working with Time  and Date variables in R Programming Language 30

It shows date in YYYY-MM-DD format.

We have following code values for date and time to show:

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)

We can use above code to show date in different formats.

format(today, format = “%B %d %Y”)

Working with Time  and Date variables in R Programming Language 31

We can change any data type to Date format by using as.Date() function . Here , we change character data type to date data type .

x <- as.Date(“1970/01/01”)  

x

Working with Time  and Date variables in R Programming Language 32

We check data type of “x” object .

class(x)

Working with Time  and Date variables in R Programming Language 33

We can also specify format to read data in given format .

as.Date(“1/15/2001”, format = “%m/%d/%Y”)

Working with Time  and Date variables in R Programming Language 34

as.Date(“April 26,2001”, format= “%B %d, %Y”)

Working with Time  and Date variables in R Programming Language 35

as.Date(“22JUN01”, format= “%d%b%y”)

Working with Time  and Date variables in R Programming Language 36

We create an object “Birth” to store date value. We store date under names of person.

Birth <- c(lara =as.Date(“1980-06-14”), katy = as.Date(“1987-09-19”), Priya = as.Date(“1990-01-22”))

Working with Time  and Date variables in R Programming Language 37

We can find month and weekday of date by using months() and weekdays() function respectively.

months(Birth)

Working with Time  and Date variables in R Programming Language 38

weekdays(Birth)

Working with Time  and Date variables in R Programming Language 39

We can find date differences between two days as :

y <- Sys.Date() – as.Date(“1980-06-14”)

y

Working with Time  and Date variables in R Programming Language 40

It shows date difference between current date and date specified.

We can also used difftime() function to find difference between dates .

We take help to know more about difftime() function.

x1 <- as.Date(“1980-06-14”)

y1 <- Sys.Date()

Working with Time  and Date variables in R Programming Language 41

?difftime

We can find week difference between two dates .

difftime(y1, x1,units = “weeks”)

Working with Time  and Date variables in R Programming Language 42

We check current time as :

z <- Sys.time()

Working with Time  and Date variables in R Programming Language 43

We create an object of POSIXlt data type as:

p <- as.POSIXlt(z)

Working with Time  and Date variables in R Programming Language 44

unclass(p)

Working with Time  and Date variables in R Programming Language 45

It shows all nine parameters as seconds , minutes , hours , day of month , month of the year , years since 1990 , day of the week ,day of the year  and daylight saving time. It includes time zone as “IST” .

It also shows gmtoff parameter which is the offset in seconds from GMT time zone.

We want to check all the parameters of POSIXlt data type.

names(unclass(p))

Working with Time  and Date variables in R Programming Language 46

We can also extract specific parameter of POSIXlt object .

We extract hour and day of the year parameter as:

p$hour

Working with Time  and Date variables in R Programming Language 47

p$yday

Working with Time  and Date variables in R Programming Language 48

We create an object of POSIXct type. It only show seconds time from January 1 , 1970.

p <- as.POSIXct(z)

unclass(p)

Working with Time  and Date variables in R Programming Language 49

We can also see time from various time zone as:

as.POSIXlt(Sys.time(), “GMT”)

Working with Time  and Date variables in R Programming Language 50

To easily work with dates , we use lubridate package . We install and load lubridate package as:

install.packages(“lubridate”)

library(lubridate)

We are using interval() function to make interval between two dates.

span <- interval(as.POSIXct(“2009-01-01”), as.POSIXct(“2010-02-02 01:01:01”))

Working with Time  and Date variables in R Programming Language 51

We find difference between two dates by using as.period() :

as.period(span)

Working with Time  and Date variables in R Programming Language 52

It shows difference between two dates as one year one month one day one hour one minute and one second.

We find difference between today and date specified.

abc <- interval( as.Date(“1980-06-14”), Sys.Date())

Working with Time  and Date variables in R Programming Language 53

as.period(abc)

Working with Time  and Date variables in R Programming Language 54

We can also find days difference between two dates by using unit parameter in as.period(). By specifying unit = “days” to find difference as days difference.

leap <- interval(ymd(“2016-01-01”), ymd(“2017-01-01”))

Working with Time  and Date variables in R Programming Language 55

as.period(leap, unit = “days”)

Working with Time  and Date variables in R Programming Language 56

It shows 366 days between two dates.

We can also find years difference between dates.

as.period(leap, unit = “years”)

Working with Time  and Date variables in R Programming Language 57

We use ymd() to read dates as yyyy-mm-dd and time zone(tz) as “America/Chicago”.

dst <- interval(ymd(“2016-11-06”, tz = “America/Chicago”),

                ymd(“2016-11-07”, tz = “America/Chicago”))

Working with Time  and Date variables in R Programming Language 58

It shows seconds difference between two dates.

Working with Time  and Date variables in R Programming Language 59

strptime ()

To convert character type to POSIXt type.

?strptime

Working with Time  and Date variables in R Programming Language 60

We create a character vector as datestring.

datestring <- c(“January 10,2012 10:40”, “December 9, 2011 9:10”)

Working with Time  and Date variables in R Programming Language 61

We convert character type to POSIXt type and also provide format to read date as given format.

x <- strptime(datestring, “%B %d, %Y %H:%M”)

x

Working with Time  and Date variables in R Programming Language 62

class(x)

Working with Time  and Date variables in R Programming Language 63

weekdays(x)

Working with Time  and Date variables in R Programming Language 64

months(x)

Working with Time  and Date variables in R Programming Language 65

We are using mdy_hm() to convert character to POSIXt type. We read as month , day , year , hour:minute  and store it in default date type as year , month and date.    

We create start and end object to store time.

start <- mdy_hm(“3-11-2017 6:25”, tz = “US/Eastern”)

Working with Time  and Date variables in R Programming Language 66

end <- mdy_hm(“3-13-2017 6:25”, tz = “US/Eastern”)

Working with Time  and Date variables in R Programming Language 67

class(start)

Working with Time  and Date variables in R Programming Language 68

class(end)

Working with Time  and Date variables in R Programming Language 69

You can notice how the time zone changes from EST to EDT indicating that Daylight Savings has started. 

We create an interval by using %–% operator.

time.interval <- start %–% end

Working with Time  and Date variables in R Programming Language 70

To create a Duration between these two dates, we can use the as.duration() function.

time.duration <- as.duration(time.interval)

Working with Time  and Date variables in R Programming Language 71

A duration object prints the elapsed time in seconds as well as something in days. It represent 1.96 days equals to 47 hours . The Daylight Savings went into effect at 2:00 AM during the interval , an hour was skipped . Thus the duration is only 47 hours.

Leave a Comment

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