ms821
ms821

Reputation: 1

Addition of POSIX dates in R

As a part of a larger project, I am trying to define two POSIX dates and calculate the middle of the time period

I have tried the following code. The first calculation works as expected (with a binary '+' for POSIX objects) resulting in

[1] "2024-06-01 23:59:30 AEST"

but the second calculation throws an error message

Error in `+.POSIXt`(period_start, period_end) : 
  binary '+' is not defined for "POSIXt" objects

I did notice that the partial calculation of (period_end - period_start) has the class difftime but I have no idea of the significance. Just can't work our what I am missing here

Example code is

library(lubridate)

period_start <- as.POSIXct("2024-05-29 00:00", format = "%Y-%m-%d %H:%M")

period_end <- as.POSIXct("2024-06-05 23:59", format = "%Y-%m-%d %H:%M")

period_middle <- period_start + (period_end - period_start) / 2

print(period_middle)

period_middle <- (period_start + period_end) / 2

print(period_middle)

Upvotes: 0

Views: 28

Answers (0)

Related Questions