I have some MATLAB serial date number that I need to use in R but I havt to convert them to a normal date.
Matlab:
datestr(733038.6)
ans =
27-Dec-2006 14:24:00
you can see it gives the date and time.
Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(733038.6)
[1] "2006-12-27"
It gives only the date but I need also the time? Any idea
The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of time for the "POSIXct" class in R is, ‘1970-01-01 00:00.00 UTC’. You can read about how different systems handle dates here.
Before converting, you need to account for this difference in reference from one format to another. The POSIX manual contains such an example. Here's my output:
> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"
Where 719529 is ‘1970-01-01 00:00.00 UTC’ in Matlab's datenum and 86400 the number of seconds in an standard UTC day.
Related
I want to convert number of days to date with time:
> 15525.1+as.Date("1970-01-01")
[1] "2012-07-04" ## correct but no time
I tried this:
> apollo.fmt <- "%B %d, %Y, %H:%M:%S"
> as.POSIXct((15525.1+as.Date("1970-01-01")), format=apollo.fmt, tz="UTC")
[1] "2012-07-04 04:24:00 CEST"
but as you see the results provide in CEST. But I need it it in UTC.
Any hints on this?
For the original conversion, refer to this question: Converting numeric time to datetime POSIXct format in R and these pages: Date-times in R , Date-time conversions and Converting excel dates (number) to R date-time object. Bascially, it depends on your data source, the time origin for that data sources (Excel, Apache etc.) and the units. For example, you may have the total time elapsed in seconds, minutes, hours or days since the time origin for your data source which will be different for Excel or Apache. Once you have this information, you can use strptime or origin arguments and convert to R date-time objects.
If you are only concerned with changing the timezone, you can use attr:
> u <- Sys.time()
> u
[1] "2017-12-21 09:01:35 EST"
> attr(u, "tzone") <- "UTC"
> u
[1] "2017-12-21 14:01:35 UTC"
You may want to check up on the valid timezones for your machine though. A good way to get a time-zone that works with your machine would be googleway::google_timezone. To get the coordinates for your location (or the location from where you're importing data), you can either look those up online or use ggmap::geocode() - useful if converting time stamps in data from different time zones.
I think the problem is as.POSIXct doesn't change anything if the time is already POSIXct, so the tz option has no effect.
Use attr as explained here
This question already has answers here:
Convert UNIX epoch to Date object
(2 answers)
Closed 5 years ago.
I have a data set where one of the columns is sales date. Don't know why, but R converts it to numeric why performing any operation. I would like to convert it back to POSIXct date format in R. To do the same, I am using below code, but getting an unexpected result
x= as.Date(1448208000, origin = "1970-01-01")
[1] "3967028-10-31"
x= as.POSIXct(x,"%Y-%m-%d")
I am not good with dates format in R and would appreciate any kind of help in this regard.
1448208000 is the number of seconds since the unix epoch, and is the numeric representation of a POSIX object. To convert it back to POSIXct you want
as.POSIXct(1448208000, origin = "1970-01-01")
You'll also probably want to ensure the timezone is correct too; see the difference between these two commands
as.POSIXct(1448208000, origin = "1970-01-01", tz = "UTC")
# [1] "2015-11-22 16:00:00 UTC"
as.POSIXct(1448208000, origin = "1970-01-01", tz = "Australia/Melbourne")
# [1] "2015-11-23 03:00:00 AEDT"
I have some integers (timestamps) and I want to convert them into time intervals (time deltas). How can I do that in R?
Something like this:
strptime(37998530.34, format="%d %H:%M")
This R code returns with error:
difftime(37998530.34, 0)
In Python I can use this code:
datetime.timedelta(0, 37998530.34)
And the result is:
datetime.timedelta(439, 68930, 340000)
439 days, 19:08:50.340000
Thank you!
In R you need to specify the origin for the date conversion. Assuming your value is the number of seconds use this function:
as.POSIXlt(37998530.34, origin="1970-01-01")
#[1] "1971-03-16 14:08:50 EST"
as.POSIXlt function also has an option to specify the desired timezone (tz= )
The following code:
difftime(as.POSIXlt(37998530.34, origin="1970-01-01"), as.POSIXlt(0, origin="1970-01-01"), units=c('days'))
has the result:
Time difference of 439.8 days
I run discrete event simulations where the time originates from dates. I think that simulations run much faster, when I convert all the dates to integers (relative time in seconds).
What is the best way, to switch between date and seconds in a well definied way where I want to
set the reference time (e.g. "1970-01-01 00:00:00 GMT" or "2016-01-01 00:00:00 GMT") manually,
the time zone and
the origin (Not possible in lubridate?)
I thought I can use the origin for this purpose but it does not influence the result:
> as.numeric(as.POSIXct("2016-01-01 00:00:00 GMT",origin="2016-01-01",tz="GMT"))
> as.numeric(as.POSIXct("2016-01-01 00:00:00 GMT",origin="1970-01-01",tz="GMT"))
both result in [1] 1451606400.
(Only the tz argument changes the result, which is ok of course:
> as.numeric(as.POSIXct("2016-01-01 00:00:00 CEST", tz= "America/Chicago"))
[1] 1451628000)
You can use difftime() to calculate the difference between some timestamp and a reference time:
as.numeric(difftime(as.POSIXct("2016-01-01 00:00:00",tz="GMT"),
as.POSIXct("1970-01-01 00:00:00",tz="GMT"), units = "secs"))
## [1] 1451606400
By choosing another value for units, you could also get the number of minutes, hours, etc.
The reason that you get the same result for both choices of origin is that this argument is only intended to be used when converting a number into a date. Then, the number is interpreted as seconds since the origin that you pass to the function.
Internally, a POSIXct object is always stored as seconds since 1970-01-01 00:00:00, UTC, independent of the origin that you specified when doing the conversion. And accordingly, converting to numeric gives the same result for any choice of origin.
You can have a look at the documentation of as.POSIXct():
## S3 method for class 'character'
as.POSIXlt(x, tz = "", format, ...)
## S3 method for class 'numeric'
as.POSIXlt(x, tz = "", origin, ...)
As you can see, origin is only an argument for the method for numeric, but not for character.
I have some MATLAB serial date number that I need to use in R but I havt to convert them to a normal date.
Matlab:
datestr(733038.6)
ans =
27-Dec-2006 14:24:00
you can see it gives the date and time.
Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(733038.6)
[1] "2006-12-27"
It gives only the date but I need also the time? Any idea
The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of time for the "POSIXct" class in R is, ‘1970-01-01 00:00.00 UTC’. You can read about how different systems handle dates here.
Before converting, you need to account for this difference in reference from one format to another. The POSIX manual contains such an example. Here's my output:
> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"
Where 719529 is ‘1970-01-01 00:00.00 UTC’ in Matlab's datenum and 86400 the number of seconds in an standard UTC day.