Adding time to POSIXct object in R
POSIXct
objects are a measure of seconds from an origin, usually the UNIX epoch (1st Jan 1970). Just add the requisite number of seconds to the object:
x <- Sys.time()
x
[1] "2012-08-12 13:33:13 BST"
x + 3*60*60 # add 3 hours
[1] "2012-08-12 16:33:13 BST"
How to add days to a POSIXct object in R
Your time format is not being tested by as.POSIXct
. This is why you get this result.
You can use the following format to get it correct:
library(lubridate)
# note the format option to get it correct
input_time = as.POSIXct("2018-05-05T14:14:05", format = "%Y-%m-%dT%H:%M:%OS")
input_time + lubridate::days(1)
[1] "2018-05-06 14:14:05 UTC"
or just use everything with functions from lubridate:
input_time <- lubridate::ymd_hms("2018-05-05T14:14:05")
input_time + lubridate::days(1)
[1] "2018-05-06 14:14:05 UTC"
Adding time to time in R
When you use the format function, you create a character variable that cannot be sum. Therefore, I would suggest to keep then as POSIXt and use as character when necessary
Also, the minutes and hours functions from lubridate create a period object that is converted to a number when in a list (I don't completely understand this part here) and I could not sum using lists.
Anyways, if I understand propperly, I have manage to get the results you wanted by working with no list (you can put the results after on one, if you may) with the code:
hourTime<-(seq.POSIXt(as.POSIXct(Sys.Date()),
as.POSIXct(Sys.Date()+1),
by = "5 min"))
ARRIVALTIME <- sample(hourTime, 1000, replace=T)
TRETTIME <- ARRIVALTIME +
(hours(sample(1:3, 1000, replace=T)) +
minutes(sample(1:60, 1000, replace=T)))
How to add/subtract time from a POSIXlt time while keeping its class in R?
Short answer: No
Long answer:
POSIXct
and POSIXlt
objects are two specific types of the more general POSIXt
class (not in a strictly object oriented inheritance sense, but in a quasi-object oriented implementation sense). Code freely switches between these. When you add to a POSIXlt
object, the actual function used is +.POSIXt
, not one specifically for POSIXlt
. Inside this function, the argument is converted into a POSIXct
and then dealt with (added to).
Additionally, POSIXct
is the number of seconds from a specific date and time. POSIXlt
is a list of date parts (seconds, minutes, hours, day of month, month, year, day of week, day of year, DST info) so adding to that directly doesn't make any sense. Converting it to a number of seconds (POSIXct
) and adding to that does make sense.
Modifying timezone of a POSIXct object without changing the display
EDITED:
My previous solution was passing a character value to origin
(i.e.origin="1970-01-01"
). That only worked here because of a bug (#PR14973) that has now been fixed in R-devel.
origin
was being coerced to POSIXct
using the tz
argument of the as.POSIXct
call, and not "GMT"
as it was documented to do. The behavior has been changed to match the documentation which, in this case, means that you have to specify your timezone for both the origin
and the as.POSIXct
call.
datetime
#[1] "2011-01-01 12:32:23.233 GMT"
as.POSIXct(as.numeric(datetime), origin=as.POSIXct("1970-01-01", tz="Europe/Paris"),
tz="Europe/Paris")
#[1] "2011-01-01 12:32:23.233 CET"
This will also works in older versions of R.
Related Topics
Shared Memory in Parallel Foreach in R
Using R to Analyze Balance Sheets and Income Statements
How to Save Summary(Lm) to a File
Ggplot2: Geom_Text() with Facet_Grid()
Update Graph/Plot with Fixed Interval of Time
Debugging (Line by Line) of Rcpp-Generated Dll Under Windows
Remove Parenthesis from a Character String
Ggplot2 Draw Dashed Lines of Same Colour as Solid Lines Belonging to Different Groups
R Xts: Generating 1 Minute Time Series from Second Events
Annotating Facet Title as Strip Over Facet
How to Round a Data.Frame in R That Contains Some Character Variables
Replace Accented Characters in R with Non-Accented Counterpart (Utf-8 Encoding)
Shiny Saving Url State Subpages and Tabs
Minimal Example of Rpy2 Regression Using Pandas Data Frame
Summing Across Rows of a Data.Table for Specific Columns