Add or subtract days in R

Add or subtract days from date or datetime in R

There are multiple ways how to add or subtract days from date in R, and here are some of them. The lubridate package is essential in these tasks. It has multiple functions that help do deal with even more difficult situations. You can easily calculate the first or last days of the month or age calculation in R.

First of all, it is important to know what object class of date or datetime you have. In my case, there is a timestamp and systems date. Use function class to see what kind of object you have.

pdt <- as.POSIXct('2021-12-17 18:00:00')
pd <- as.POSIXct('2021-12-17')
 
class(pd)
#[1] "POSIXct" "POSIXt" 
 
Sys.Date()
#[1] "2021-12-17"
 
class(Sys.Date())
#[1] "Date"

 

Add or subtract days from date in R base

If you have a POSIXct object, you can add or subtract days arithmetically by using the number of seconds in one day. The same time in the next day will look like this.

pdt + 1*24*60*60
#[1] "2021-12-18 18:00:00 EET"

By adding or subtracting the different number of seconds, you can change the time component differently. If the time component is not necessary, you can remove it in multiple ways that you can find here.

In this case, I already have a POSIXct date object and the next or previous date will look like this.

pd + 1*24*60*60
#[1] "2021-12-18 EET"

pd - 1*24*60*60
#[1] "2021-12-16 EET"

If you have an object of class “Date” like systems date, you can add or subtract days in R by simply adding or subtracting 1.

class(Sys.Date())
#[1] "Date"

Sys.Date() + 1
#[1] "2021-12-18"

Sys.Date() - 1
#[1] "2021-12-16"

 

Add or subtract days in R by using the lubridate package

No matter if you have a POSIXct or Date object you can add or subtract days with help of lubridate very easily.

require(lubridate)

pd + days(1)
#[1] "2021-12-18 EET"

pd - days(1)
#[1] "2021-12-16 EET"

The class of result of this calculation with lubridate stays the same.

class(pd + days(1))
#[1] "POSIXct" "POSIXt"

If it is necessary to get POSIXct object with lubridate you can use a function like ymd that can parse that.

class(ymd('2021-12-17'))
#[1] "Date"

class(ymd_hms('2021-12-17 18:00:00'))
#[1] "POSIXct" "POSIXt"

 

Add months or years to date in R

If you have to add or subtract months or years in R, then you can also do that easily with lubridate. To do that, use months and years functions.

pd
#[1] "2021-12-17 EET"
 
pd + months(1)
#[1] "2022-01-17 EET"
 
pd + months(-1)
#[1] "2021-11-17 EET"
 
pd + years(1)
#[1] "2022-12-17 EET"
 
pd + years(-1)
#[1] "2020-12-17 EET"

If you are getting the error “Error in as.POSIXlt.numeric(x, tz = tz(x)) : ‘origin’ must be supplied” I think you are using function year that is different from function years.

 

I like the lubridate package. It is fast enough and the spectrum of available options. You can manage situations that are challenging for base R like ISO year and ISO week calculations, age calculations, or weekdays.





Posted

in

Comments

Leave a Reply

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