[R] Problem with fractional seconds

Gabor Grothendieck ggrothendieck at gmail.com
Wed May 27 00:55:47 CEST 2009


POSIXct is intended to represent date/times.  You can use format to just
show the times but the dates will still be there:

> format(Sys.time(), "%H:%M:%S%OS")
[1] "18:20:0000.910"

difftime objects can represent times although perhaps not
conveniently:

> now <- Sys.time()
> now - as.POSIXct(cut(now, "day"))
Time difference of 18.35699 hours

chron objects use the integer part as days and the
fractional part as fractions of days so can represent
whatever granularity you like up to machine precision
although they won't print it out by default but its there
as can be seen:

> now <- Sys.time(); now
[1] "2009-05-26 18:52:12.221 EDT"
> tt <- times(as.chron(now, tz = "")) %% 1; tt
[1] 18:52:12
> # tt minus tt truncated to the sec still leaves subsecs
> as.numeric(tt - trunc(tt, "00:00:01")) * 24 * 60 * 60
[1] 0.2209999

On Tue, May 26, 2009 at 6:14 PM, Tim Clark <mudiver1200 at yahoo.com> wrote:
>
> Gabor,
>
> Thanks, that worked.  However, is there is way to just get the time and not have the date added?  I assume the date is added since POSIX is based on seconds since 1970.  I can't seem to convert the POSIX value to Chron times(), and Chron won't take fractional seconds.  Are there other ways to deal with just time and not date?
>
> Thanks,
>
> Tim
>
> Tim Clark
> Department of Zoology
> University of Hawaii
>
>
> --- On Tue, 5/26/09, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>
>> From: Gabor Grothendieck <ggrothendieck at gmail.com>
>> Subject: Re: [R] Problem with fractional seconds
>> To: "Tim Clark" <mudiver1200 at yahoo.com>
>> Cc: r-help at r-project.org
>> Date: Tuesday, May 26, 2009, 11:59 AM
>> Try this:
>>
>> > as.POSIXct(c("06:00:00.100","06:00:01.231"), format =
>> "%H:%M:%S%OS")
>> [1] "2009-05-26 06:00:00.100 EDT" "2009-05-26 06:00:00.231
>> EDT"
>>
>>
>> On Tue, May 26, 2009 at 5:52 PM, Tim Clark <mudiver1200 at yahoo.com>
>> wrote:
>> >
>> > Dear List,
>> >
>> > I am having problems converting a file with fractional
>> seconds to class POSIXct.  I have set my options to include
>> digits.secs and my format to just time, but my output is the
>> current date with my time lacking the fractions of a second.
>>  For example:
>> >
>> > options(digits.secs=3)
>> > t<-c("06:00:00.100","06:00:01.231")
>> > myt<-as.POSIXct(t,format="%H:%M:%S")
>> > myt
>> >
>> > [1] "2009-05-26 06:00:00 HST" "2009-05-26 06:00:01
>> HST"
>> >
>> > I would like the output to be just time with
>> fractional seconds. I.e.
>> >
>> > 06:00:00.100,06:00:01.231
>> >
>> > I have also tried Chron times() which did not work
>> either.  Interestingly, Sys.time() does produce fractional
>> seconds, so I know the options are working.
>> >
>> > I would appreciate your help and suggestions.
>> >
>> > Aloha,
>> >
>> > Tim
>> >
>> >
>> >
>> >
>> >
>> > Tim Clark
>> > Department of Zoology
>> > University of Hawaii
>> >
>> > ______________________________________________
>> > 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