[R] Excel date to R format

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 13 02:19:41 CEST 2010


Try this:

> sprintf("%.3f", as.numeric(as.POSIXct(39965.004,origin=as.Date("1970-1-1"))))
[1] "39965.004"

> getOption("digits.secs")
NULL
> as.POSIXct(39965.004,origin=as.Date("1970-1-1"))
[1] "1970-01-01 06:06:05 EST"
> sprintf("%.3f", as.numeric(as.POSIXct(39965.004,origin=as.Date("1970-1-1"))))
[1] "39965.004"
> format(as.POSIXct(39965.004,origin=as.Date("1970-1-1")), "%Y-%m-%d %H:%M:%S %OS3")
[1] "1970-01-01 06:06:05 05.004"
>
>
> options(digits.secs = 3)
> as.POSIXct(39965.004,origin=as.Date("1970-1-1"))
[1] "1970-01-01 06:06:05.004 EST"


On Mon, Apr 12, 2010 at 7:52 PM, Ben Bolker <bolker at ufl.edu> wrote:
> Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:
>
>>
>> See the relevant article in R News 4/1.
>>
>> On Mon, Apr 12, 2010 at 10:36 AM, ManInMoon wrote:
>> >
>> > I have a vector of double like this from Excel.
>> >
>> > 39965.0004549653
>> >
>> > and I want to put them in R such that I can display them in any Date and
>> > Time format.
>> >
>
>  Emphasis:
>
>> > as.Date does it ALMOST but chops off the fractional seconds.
>> >  POSIXct doesn't
>> > appear to do what I need.
>
>  (ManInMoon: why not?  A reproducible example would help.)
>
>
>  Gabor,
>
>  I looked at that R News article but can't figure out whether *any*
> of these classes preserve sub-second resolution (which seems crazy to me;
> I guess in the case of POSIXt there may be an ANSI standard that specifies
> that 'seconds' are stored as integers, but otherwise this seems like
> a needless restriction).  Am I missing something?
>
>  Ben
>
>> as.numeric(as.POSIXct(39965.004,origin=as.Date("1970-1-1")))
> [1] 39965.00
>
> ______________________________________________
> 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