I am trying to subtract an hour (3600 s) from this time object that is defined as 01:00. When I do so, the time component disappears, and I am left with the date only. I need to preserve the time component--how do I do so? This only happens when the result of my subtraction is 00-00.
test <- strptime("2016-09-02_01-00", format = "%Y-%m-%d_%H-%M", tz = "UTC")
test
[1] "2016-09-02 01:00:00 UTC"
test-3600
[1] "2016-09-02 UTC"
This is a difference between content and representation.
Subtracting 3600 does change the structure (from
POSIXlt
toPOSIXct
) ...... but the change in formatting is just due to R trying to be helpful and printing the simplest representation available. The time information hasn't actually gone away. From
?strptime
(thanks @DavidArenburg):As @MrFlick states in comments, you can override this by specifying a format string ...