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

peter dalgaard pdalgd at gmail.com
Mon Jul 18 16:08:34 CEST 2011


On Jul 18, 2011, at 15:48 , Gabor Grothendieck wrote:

> On Mon, Jul 18, 2011 at 8:56 AM, peter dalgaard <pdalgd at gmail.com> wrote:
>> 
>> On Jul 18, 2011, at 14:08 , Gabor Grothendieck wrote:
>> 
>>> 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"
>> 
>> Doesn't work on a Mac, and in general, I think it depends on a quirk in your OS's date conversion utilities. What does work for me is
> 
> Did you have zoo 1.7-1 loaded?  What happens when you try it?


I did, actually:

> packageVersion("zoo")
[1] ‘1.7.1’
> as.Date('00-00-0000')
Error in charToDate(x) : 
  character string is not in a standard unambiguous format
> as.Date('0000-00-00')
Error in charToDate(x) : 
  character string is not in a standard unambiguous format
> as.Date(1, origin='0000-00-00')
Error in charToDate(x) : 
  character string is not in a standard unambiguous format
> as.Date(730456, origin='0000-00-00')
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

.....

However, this was after reinstalling and reloading zoo. Restarting R and retrying did indeed make things work.

>> 
>> I fail to see what "zoo" has to do with this at all!
> 
> zoo has its own as.Date.numeric method which accepts a superset of
> inputs that base::as.Date.numeric accepts.

Aha. Thanks.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list