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.
We can check today’s date by using Sys.Date() function.
today <- Sys.Date()
today
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”)
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
We check data type of “x” object .
class(x)
We can also specify format to read data in given format .
as.Date(“1/15/2001”, format = “%m/%d/%Y”)
as.Date(“April 26,2001”, format= “%B %d, %Y”)
as.Date(“22JUN01”, format= “%d%b%y”)
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”))
We can find month and weekday of date by using months() and weekdays() function respectively.
months(Birth)
weekdays(Birth)
We can find date differences between two days as :
y <- Sys.Date() – as.Date(“1980-06-14”)
y
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()
?difftime
We can find week difference between two dates .
difftime(y1, x1,units = “weeks”)
We check current time as :
z <- Sys.time()
We create an object of POSIXlt data type as:
p <- as.POSIXlt(z)
unclass(p)
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))
We can also extract specific parameter of POSIXlt object .
We extract hour and day of the year parameter as:
p$hour
p$yday
We create an object of POSIXct type. It only show seconds time from January 1 , 1970.
p <- as.POSIXct(z)
unclass(p)
We can also see time from various time zone as:
as.POSIXlt(Sys.time(), “GMT”)
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”))
We find difference between two dates by using as.period() :
as.period(span)
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())
as.period(abc)
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”))
as.period(leap, unit = “days”)
It shows 366 days between two dates.
We can also find years difference between dates.
as.period(leap, unit = “years”)
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”))
It shows seconds difference between two dates.
strptime ()
To convert character type to POSIXt type.
?strptime
We create a character vector as datestring.
datestring <- c(“January 10,2012 10:40”, “December 9, 2011 9:10”)
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
class(x)
weekdays(x)
months(x)
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”)
end <- mdy_hm(“3-13-2017 6:25”, tz = “US/Eastern”)
class(start)
class(end)
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
To create a Duration between these two dates, we can use the as.duration() function.
time.duration <- as.duration(time.interval)
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.