I have a column with seconds. The start date is 09/01/2017 01:37:33. I would like to replace the seconds with the date based on the calculations (taking into account the start date). But I couldn't find any answer to this question... Can someone help me, please?
Asked
Active
Viewed 370 times
-2
-
Possible duplicate of [R lubridate converting seconds to date](https://stackoverflow.com/questions/13998206/r-lubridate-converting-seconds-to-date) – Anonymous coward Oct 17 '18 at 18:34
2 Answers
2
Convert to POSIXct and add the number of seconds. seconds
can be 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"
G. Grothendieck
- 211,268
- 15
- 177
- 297
1
We convert the start date to as.Posixct
and set it as origin when converting seconds to date -
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"
Shree
- 9,963
- 1
- 11
- 31