[R] How to convert number (matlab) to date

Dennis Murphy djmuser at gmail.com
Sun Jul 17 14:27:01 CEST 2011


Hi:

The default time origin in R is '1970-01-01'.

as.Date('1999-12-02') - as.Date('1970-01-01')
Time difference of 10927 days

Therefore, the difference in days between Matlab's and R's origins is
10927 - 730456
[1] -719529

as.Date(-719529, origin = '1970-01-01')
[1] "000/-12-31"

If we try this as an origin,
> as.Date(730456, origin = "000/-12-31")
Error in charToDate(x) :
  character string is not in a standard unambiguous format

Checking the as.Date() help page, we find the line
"Years before 1CE (aka 1AD) will probably not be handled correctly."
However, if we add a day,

as.Date(730456, origin = '0000-01-01')
[1] "1999-12-03"
which is one day later. So it appears that by subtracting 1 from the
Matlab date and using origin '0000-01-01' should work.

Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(730456)
[1] "1999-12-02"

HTH,
Dennis


On Sat, Jul 16, 2011 at 8:50 PM, Eduardo M. A. M. Mendes
<emammendes at gmail.com> wrote:
> Hello
>
> I am new to R and I need to convert some dates (numeric format by matlab) to actual dates in R.
>
> For instance,
>
> Matlab -> 730456 -> >> datestr(730456)
>
> ans =
>
> 02-Dec-1999
>
> R -
>
>> library(zoo)
>> as.Date(730456)
> [1] "3969-12-03"
>
> I don't not mind the output format but it needs to be right.
>
>
> Many thanks
>
> Ed
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list