How to compare time in R?
Last Updated :
15 Dec, 2021
R programming Language supports both date and DateTime objects using various different formats and specifiers. The built-in framework as.Date function is responsible for the handling of dates alone, the library chron in R Programming handles both dates and times, without any support for time zones; whereas the POSIXct and POSIXlt classes provides the support for handling datetime objects as well as timezones. Easy conversion of date time objects can be performed to other date related objects.
A date string can be first converted to a POSIXct objects and then basic arithmetic can be performed on it easily. POSIXct objects ease the process of mathematical operations, since they rely on seconds as the major unit of time management. The dates are converted to standard time zone, UTC. A string type date object can be converted to POSIXct object, using the as.POSIXct(date) method in R. Since, the dates are stored on seconds, the subtraction can be performed by first converting the hours and minutes to the units of seconds too.
1 hour = 1 * 60 * 60 seconds
1 min = 1 * 60 seconds
Method 1 : Using logical operators
POSIXct dates can be compared using the logical operators to compare dates. Logical operators are applied in between the different argument dates and a boolean TRUE or FALSE value is returned based on the output.
Example:
R
# declaring a time object
time1 <- as.POSIXct("08:32:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("08:32:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
if(time1< time2){
print ("Time1 smaller")
}else{
print ("Time2 smaller")
}
}
Output
[1] "Time 1"
[1] "2021-05-18 08:32:07 UTC"
[1] "Time 2"
[1] "2021-05-18 08:32:08 UTC"
[1] "Time1 smaller"
Method 2 : Using comparison operators
The difftime() method in R language is used to compute the difference in the timestamps given. It is used to return an object of the class difftime itself accompanied by a units attribute. "difftime" objects support only limited arithmetic operations upon them, that is, they can be added or subtracted, and multiplied or divided by a numeric vector. The result returned by this method is based on the first parameter time stamp value subtracted by second parameter, that is time1-time2. The result is positive in case the time1 is larger than time2, 0 if the two time frames are equal and negative for the remaining case.
Syntax: difftime(time1, time2, tz,units = c("auto", "secs", "mins", "hours","days", "weeks"))
Parameter :
- time1 and time2 - the datetime objects or numeric vectors
- tz - time zone (optional)
- units - the specification of units to perform arithmetic on
Return type : a difftime object applying the arithmetic on datetime object
Example :
R
# declaring a time object
time1 <- as.POSIXct("08:35:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("08:32:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
# checking if time1 is smaller than time2
if(time1< time2){
print ("Time2 - Time1")
# calculating time2-time1
difftime(time2,time1, units = "hours")
}else{
# calculating time1-time2
print ("Time1 - Time2")
difftime(time1,time2, units = "hours")
}
}
Output
[1] "Time 1"
[1] "2021-05-18 08:35:07 UTC"
[1] "Time 2"
[1] "2021-05-18 08:32:08 UTC"
[1] "Time1 - Time2"
Time difference of 0.04972222 hours
The date objects can be compared using the minus operator to subtract the smaller date from the larger date. The units in which the date is returned is the maximum of the hours, minutes or seconds.
Example:
R
# declaring a time object
time1 <- as.POSIXct("09:35:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("09:35:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
# checking if time1 is smaller than time2
if(time1< time2){
print ("Time2 - Time1")
# calculating time2-time1
print (time2 -time1)
}else{
# calculating time1-time2
print ("Time1 - Time2")
print (time1-time2)
}
}
Output
[1] "Time 1"
[1] "2021-05-18 09:35:07 UTC"
[1] "Time 2"
[1] "2021-05-18 09:35:08 UTC"
[1] "Time2 - Time1"
Time difference of 1 secs
Similar Reads
How to Compare Time in MS SQL Server? To compare time in MS SQL Server, use the comparison operators (=,<,>, etc.). In this article, we will be making use of the Microsoft SQL Server as our database. and comparing times using both pre-defined dates and the current date and time with the GETDATE() function.First, let's create a dat
3 min read
How to merge date and time in R? The date and time objects in R programming language can be represented using character strings in R. The date and time objects can be merged together using POSIX format or in the form of datetime objects. The POSIXlt class stores date and time information. Discussed below are various approaches to c
3 min read
How to separate date and time in R ? In this article, we are going to separate date and time in R Programming Language.  Date-time is in the format of date and time (YYYY/MM/DD HH:MM:SS- year/month/day Hours:Minute:Seconds). Extracting date from timestamp: We are going to extract date by using as.Date() function. Syntax:  as.Date(data
2 min read
How to subtract time in R ? In this article, we will discuss how to subtract time in R Programming Language. Method 1: Using difftime() method in R The difftime() method in R is used to compute the difference in the timestamps given. It is used to return an object of the class difftime itself accompanied by units attribute. "d
4 min read
How to Convert Character to a Timestamp in R? In this article, we will convert character to timestamp in R Programming Language. We can convert the character to timestamp by using strptime() method. strptime() function in R Language is used to parse the given representation of date and time with the given template. Syntax:Â strptime(character,
1 min read
How to Convert Date to Numeric in R? In this article, we will discuss how to convert date to numeric in R Programming Language. Method 1: Using as.numeric() This function is used to convert date into numeric Syntax: as.numeric(date) where the date is the input date. Example: R data = as.POSIXct("1/1/2021 1:05:00 AM", format="%m/%d/%Y %
2 min read