Open In App

Subtract hours from time in R

Improve
Improve
Like Article
Like
Save
Share
Report

The time objects in R programming language can be declared either using POSIXct class, which offers fast manipulation and storage of such objects. External packages in R also help in working with time and dates and allow both comparison and direct arithmetic operations to be performed upon them. 

Method 1 : Using POSIXct object

A date string can be first converted to 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 the 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

Syntax:

as.POSIXct ( date , format)

The result is returned as a datetime object with the respective number of hours subtracted belonging to the POSIXct class. 

Example:

R




# declaring a time object
time1 <- as.POSIXct("08:32:07", format = "%H:%M:%S")
  
print ("Time")
print (time1)
  
# converting 3 hours to seconds
hrs <- 3 * 60 * 60
  
print  ("Subtracted Time")
print (time1 - hrs)


Output

[1] "Time"
[1] "2021-05-19 08:32:07 UTC"
[1] "Subtracted Time"
[1] "2021-05-19 05:32:07 UTC"

Method 2 : Using lubridate package

Lubridate package in R is used to work with date and time objects. It makes it easier to parse and manipulate the objects and needs to be installed and loaded into the working space.

The Sys.time() function in R is used to fetch the current date and time object according to the IST zone.  The hours() method in R is used to take an input integer denoting the number of hours. The lubridate package objects allow direct arithmetic over their various components, therefore the number of hours can be directly subtracted from the lubricate time object. A result is also an object belonging to this class. 

Example:

R




# getting required libraries
library(lubridate)
  
# getting current time
time <- Sys.time()
  
print("Current time")
print (time)
  
# subtracting hours
hrs <- hours(5)
mod_time <- time - hrs
  
print ("Subtracting 5 hours")
print (mod_time)


Output

[1] "Current time" 
[1] "2021-05-19 16:31:37 IST" 
[1] "Subtracting 5 hours" 
[1] "2021-05-19 11:31:37 IST"

Method 3 : Using strptime() method

strptime method in R is used to directly convert character vectors (of a variety of formats) to POSIXlt format. strptime is faster than the previous approach because strptime() only handles character input.

Syntax:

strptime(date, format, tz = “”)

Parameter :

date – The date in character format

format – The format specifier of the input date

tz – time zone (optional)

strptime() works similar to the POSIXct objects, where all the calculations are done in terms of seconds. Also, the date is returned which is essential in the cases, where the subtraction of certain hours results in the change of date too. 

Example:

R




# declaring a time object
time1 <- strptime("00:32:07", format = "%H:%M:%S")
print ("Time")
print (time1)
  
# converting 5 hours to seconds
hrs <- 5 * 60 * 60
print  ("Subtracted Time")
print (time1 - hrs)


Output

[1] "Time" 
[1] "2021-05-19 00:32:07 IST"
[1] "Subtracted Time" 
[1] "2021-05-18 19:32:07 IST"


Last Updated : 30 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads