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
You need to specify the origin:
as.Date(730456, origin = "matlabs origin date")
HTH,
Josh
P.S. Alternately, you may be able to do something like:
## find R's numeric representation of 02-Dec-1999 and use the
difference from Matlabs to offset
MatLabROffset <- 730456 - as.numeric(as.Date("1999-12-02"))
as.Date(730456 - MatLabROffset)
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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/
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.
>
On Sat, Jul 16, 2011 at 11: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 >Set the origin to Matlab's origin like this. Be sure you are using the indicated version of zoo or later:> library(zoo) > packageVersion("zoo")[1] ?1.7.1?> as.Date(730456, origin = "0000-00-00")[1] "1999-12-02" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Reasonably Related Threads
- readMat - how to retrieve the variables
- help with POSIX
- try / tryCatch for download.file( ) within a for loop when URL does not exist
- String concatenation, File Path Handling to pass to download.file( ) [backslash in DOS paths]
- Regression - how to deal with past values?