[R] How to convert number (matlab) to date
djmuser at gmail.com
Sun Jul 17 14:27:01 CEST 2011
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
as.Date(-719529, origin = '1970-01-01')
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')
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')
On Sat, Jul 16, 2011 at 8:50 PM, Eduardo M. A. M. Mendes
<emammendes at gmail.com> wrote:
> 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 =
> R -
>  "3969-12-03"
> I don't not mind the output format but it needs to be right.
> Many thanks
> R-help at r-project.org mailing list
> 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