Home » How to Subtract Hours from Time in R (With Examples)

How to Subtract Hours from Time in R (With Examples)

by Erma Khan

You can use one of the following methods to subtract a certain number of hours from a time in R:

Method 1: Use Base R

#create new column that subtracts 4 hours from time
df$subtract4 4*3600)

Method 2: Use lubridate Package

library(lubridate)

#create new column that subtracts 4 hours from time
df$subtract4 4)

The following examples show how to use each method with the following data frame:

#create data frame
df frame(time=as.POSIXct(c('2022-01-03 08:04:15', '2022-01-05 14:04:15',
                                   '2022-01-05 20:03:53', '2022-01-06 03:29:13',
                                   '2022-01-06 06:15:00', '2022-01-07 10:48:11'),
                                   format='%Y-%m-%d %H:%M:%OS'),
                 sales=c(130, 98, 240, 244, 174, 193))

#view data frame
df

                 time sales
1 2022-01-03 08:04:15   130
2 2022-01-05 14:04:15    98
3 2022-01-05 20:03:53   240
4 2022-01-06 03:29:13   244
5 2022-01-06 06:15:00   174
6 2022-01-07 10:48:11   193

Note: To add hours to a date, simply change the subtraction sign to an addition sign in either of the formulas above.

Example 1: Subtract Hours from Time Using Base R

The following code shows how to create a new column called subtract4 that subtracts four hours from each value in the time column:

#create new column that subtracts 4 hours from time
df$subtract4 4*3600)

#view updated data frame
df

                 time sales           subtract4
1 2022-01-03 08:04:15   130 2022-01-03 04:04:15
2 2022-01-05 14:04:15    98 2022-01-05 10:04:15
3 2022-01-05 20:03:53   240 2022-01-05 16:03:53
4 2022-01-06 03:29:13   244 2022-01-05 23:29:13
5 2022-01-06 06:15:00   174 2022-01-06 02:15:00
6 2022-01-07 10:48:11   193 2022-01-07 06:48:11

Notice that the values in the new subtract4 column are equal to the values in the time column with four hours subtracted from them.

Note: We use (4*3600) in the formula because time values are stored as seconds in R. Since there are 3,600 seconds in one hour, we must multiply 4 by 3,600 to subtract 4 hours.

Example 2: Subtract Hours from Time Using lubridate Package

The following code shows how to use the hours() function from the lubridate package to create a new column called subtract4 that subtracts four hours from each value in the time column:

library(lubridate)

#create new column that subtracts 4 hours from time
df$subtract4 4)

#view updated data frame
df

                 time sales           subtract4
1 2022-01-03 08:04:15   130 2022-01-03 04:04:15
2 2022-01-05 14:04:15    98 2022-01-05 10:04:15
3 2022-01-05 20:03:53   240 2022-01-05 16:03:53
4 2022-01-06 03:29:13   244 2022-01-05 23:29:13
5 2022-01-06 06:15:00   174 2022-01-06 02:15:00
6 2022-01-07 10:48:11   193 2022-01-07 06:48:11

Notice that the values in the new subtract4 column are equal to the values in the time column with four hours subtracted from them.

Note that this method produces the same results as the base R method.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Convert Date to Numeric in R
How to Extract Month from Date in R
How to Add and Subtract Months from a Date in R

Related Posts