I have measurements that were taken at this time: 13880 and they represent "days since 1970-01-01 00:00:00"
So now I want to know the dat and time:
as.Date(13880, origin="1970-01-01")
[1] "2008-01-02" # works fine
Now to add the time:
as.Date(13880, origin="1970-01-01",tz = "UTC", format="%Y/%m/%d %H:%M:%S")
[1] NA
or
as.POSIXct(13880, origin="1970-01-01")
[1] "1970-01-01 04:51:20 CET"
as.POSIXlt(13879, origin="1970-01-01")
[1] "1970-01-01 04:51:19 CET"
None of these worked for me. Any idea?
as.POSIXct(as.Date("1970-01-01") + 13880) # returns "2008-01-01 19:00:00 EST"
as.POSIXct(as.Date("1970-01-01") + 13880.5) # returns "2008-01-02 07:00:00 EST"
You can also set your time zone:
How to change the default time zone in R?
also: http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html
Related
I have a date-time column in my database in a format of "2017-01-02 8:27" as example. I want to add 10 minutes to this date-time version.
dat$EventTime=as.POSIXct(strptime( dat$EventTime, "%Y-%m-%d %H:%M"), tz = "", origin = '1970-01-01 00:00')
--date-time format becomes 2017-01-02 08:27:00 which is ok, however when I try to add 10 minutes
dat$EventTime[1]+minute(10)
--I come across with this error
Error in as.POSIXlt.numeric(x) : 'origin' must be supplied
--Could you please help me with that issue?
Just use anytime() from the anytime package which does this without requiring format or origin:
R> anytime("2017-01-02 08:27")
[1] "2017-01-02 08:27:00 CST"
R>
The resulting object is of course POSIXct so can calculate at at will:
R> anytime("2017-01-02 08:27") + seq(1,6)*60
[1] "2017-01-02 08:28:00 CST" "2017-01-02 08:29:00 CST" "2017-01-02 08:30:00 CST"
[4] "2017-01-02 08:31:00 CST" "2017-01-02 08:32:00 CST" "2017-01-02 08:33:00 CST"
R>
Edit: And I just realized that we used a single digit '8' which the default (Boost) parser does not like. Correcting to '08' now.
In base R, you can do
as.POSIXct("2017-01-02 8:27", format = "%Y-%m-%d %H:%M", tz = "UTC") + 10 * 60
#[1] "2017-01-02 08:37:00 UTC"
Or using lubridate
library(lubridate)
ymd_hm("2017-01-02 8:27") + minutes(10)
To do this for entire column replace string with column name
as.POSIXct(dat$EventTime, format = "%Y-%m-%d %H:%M", tz = "UTC") + 10 * 60
I have col of number of days since 1970-01-01 00:00:00 UTC that I want to convert to date and time UTC.
I tried this:
z <- Sys.time()
j=floor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)
as.POSIXct(j, origin = "1970-01-01")
[1] "1970-01-01 05:53:22 CET"
But it is not correct. any idea?
You have to pass to the function as.POSIXct the number of seconds, try this code
j<-17148.5625000000
as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC")
[1] "2016-12-13 13:30:00 UTC"
If you want only the day in output
as.Date(as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC"))
[1] "2016-12-13"
I want to join a list of time zones into one long vector. Append seems to convert the time zones into the system time zone? Why?
> times = list(as.POSIXct("2015-01-01", tz = 'UTC', origin = "1970-01-01"),
+ as.POSIXct("2015-01-02", tz = 'UTC', origin = "1970-01-01"))
> times
[[1]]
[1] "2015-01-01 UTC"
[[2]]
[1] "2015-01-02 UTC"
> do.call(append, times)
[1] "2014-12-31 19:00:00 EST" "2015-01-01 19:00:00 EST"
I can't use unlist as it strips the list of the POSIXct class. What is the alternative?
Suppose I pass "2015-01-01 01:50:50", then it should return "2015-01-01 01:00:00" and "2015-01-01 02:00:00". How to calculate these values in R?
Assuming your time were a variable "X", you can use round or trunc.
Try:
round(X, "hour")
trunc(X, "hour")
This would still require some work to determine whether the values had actually been rounded up or down (for round). So, If you don't want to have to think about that, you can consider using the "lubridate" package:
X <- structure(c(1430050590.96162, 1430052390.96162), class = c("POSIXct", "POSIXt"))
X
# [1] "2015-04-26 17:46:30 IST" "2015-04-26 18:16:30 IST"
library(lubridate)
ceiling_date(X, "hour")
# [1] "2015-04-26 18:00:00 IST" "2015-04-26 19:00:00 IST"
floor_date(X, "hour")
# [1] "2015-04-26 17:00:00 IST" "2015-04-26 18:00:00 IST"
I would go with the following wrapper using base R (you can specify your time zone using the tz argument within the strptime function)
Myfunc <- function(x){x <- strptime(x, format = "%F %H") ; c(x, x + 3600L)}
Myfunc("2015-01-01 01:50:50")
## [1] "2015-01-01 01:00:00 IST" "2015-01-01 02:00:00 IST"
I have a vector of POSIXct objects:
> dates <- seq(as.POSIXct("2004-01-01", tz="EST"), as.POSIXct("2004-01-02", tz="EST"), as.difftime(6, units="hours"))
> dates
[1] "2004-01-01 00:00:00 EST" "2004-01-01 06:00:00 EST"
[3] "2004-01-01 12:00:00 EST" "2004-01-01 18:00:00 EST"
[5] "2004-01-02 00:00:00 EST"
I create an epoch variable that defines a POSIXct object for the UNIX epoch:
> epoch <- strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S", tz="EST")
> class(epoch)
[1] "POSIXct" "POSIXt"
> epoch
[1] "1970-01-01 EST"
I then loop through the dates vector and print out the value, offset from epoch:
> for (d in dates) { print(as.POSIXct(d, origin=epoch, tz="EST")) }
[1] "2004-01-01 05:00:00 EST"
[1] "2004-01-01 11:00:00 EST"
[1] "2004-01-01 17:00:00 EST"
[1] "2004-01-01 23:00:00 EST"
[1] "2004-01-02 05:00:00 EST"
There seems to be a five-hour offset error between the values in dates and the representation of those same values, relative to epoch.
There is a +5 hr difference between EST and UTC, but I specified the EST time zone for epoch with the tz option. Printing out epoch, there doesn't seem to be the time information, only the date.
Is there a bug with strptime or as.POSIXct, or am I calculating the offset or generating epoch incorrectly?
As mentioned in the answer to For loop style has effect on class coercion?, in the for loop, your dates are converted to numbers. That is the number of seconds since the "standard" epoch. This includes the 5 hour shift between EST and UTC. That is added as an offset to your epoch. See the source of as.POSIXct.numeric.
The following does work because it sets up dates which will be the right number of seconds when converted to numeric.
dates <- seq(as.POSIXct("2004-01-01", tz="UTC"),
as.POSIXct("2004-01-02", tz="UTC"),
as.difftime(6, units="hours"))
epoch <- strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S", tz="EST")
for (d in dates) { print(as.POSIXct(d, origin=epoch, tz="EST")) }
Which gives
[1] "2004-01-01 EST"
[1] "2004-01-01 06:00:00 EST"
[1] "2004-01-01 12:00:00 EST"
[1] "2004-01-01 18:00:00 EST"
[1] "2004-01-02 EST"