Converting dates to seconds since UNIX epoch using lubridate in R



In my dataset, I have a column that represents time in seconds. The initial date for this column is 09/01/2017 01:37:33. I am looking for a way to replace the seconds with the corresponding date by utilizing calculations that factor in the start date. However, I have been unsuccessful in finding a solution to this problem. Would someone be able to offer me assistance?

Solution 1:

Transform into POSIXct and include a specified amount of seconds using


. This can be achieved with a vector of seconds.

seconds <- 2
as.POSIXct("09/01/2017 01:37:33", format = "%m/%d/%Y %H:%M:%S") + seconds
## [1] "2017-09-01 01:37:35 EDT"

Solution 2:

The origin for converting seconds to date is set by converting the start date to



origin <- as.POSIXct("09/01/2017 01:37:33", format = "%m/%d/%Y %H:%M:%S")
# "2017-09-01 01:37:33 EDT"
seconds <- 1:5
as.POSIXct(seconds, origin = origin)
[1] "2017-08-31 21:37:34 EDT" "2017-08-31 21:37:35 EDT" "2017-08-31 21:37:36 EDT" "2017-08-31 21:37:37 EDT" "2017-08-31 21:37:38 EDT"

Frequently Asked Questions

Posted in Uncategorized