One thing to watch out for using POSIXct is 1us is about the limit of
accuracy due to floating point (see FAQ 7.31).  Notice that printing
out today's date requires about 15 digits with microsecond
granularity.  Notice in the example that if the time difference
between intervals is 0.1 us, you have exceeded the limit of precision
with POSIXct.

If you need subsecond granularity, then maybe you want to break your
timing up into a variable with 'days' and then another with time
within a day, or create a different base for POSIXct:

> x <- as.POSIXct("2011-05-01 17:55:23.123456")
> x
[1] "2011-05-01 17:55:23 EDT"
> xc <- as.numeric(x)
> print(xc, digits=20)
[1] 1304286923.123456
> xc <- xc + seq(0, by = 0.000001, length = 20)
> diff(xc)  # about 1us granularity
 [1] 0.0000009536743 0.0000009536743 0.0000011920929 0.0000009536743
0.0000009536743 0.0000009536743
 [7] 0.0000009536743 0.0000011920929 0.0000009536743 0.0000009536743
0.0000009536743 0.0000009536743
[13] 0.0000011920929 0.0000009536743 0.0000009536743 0.0000009536743
0.0000009536743 0.0000009536743
[19] 0.0000011920929
> xc <- as.numeric(x)
> xc <- xc + seq(0, by = 0.0000001, length = 20) # by 0.1 us
> diff(xc)  # notice loss of precision
 [1] 0.0000000000000 0.0000002384186 0.0000000000000 0.0000002384186
0.0000000000000 0.0000002384186
 [7] 0.0000000000000 0.0000000000000 0.0000002384186 0.0000000000000
0.0000002384186 0.0000000000000
[13] 0.0000000000000 0.0000002384186 0.0000000000000 0.0000002384186
0.0000000000000 0.0000002384186
[19] 0.0000000000000
>


On Sun, May 1, 2011 at 2:52 PM, Rainer Stuetz <rainer.stu...@gmail.com> wrote:
> On Sun, May 1, 2011 at 15:33, Joel Reymont <joe...@gmail.com> wrote:
>>
>> Does R have support for microseconds in timestamps, e.g. when reading this in
>>
>> "Time","Include","Kind","Duration"
>> 2011-04-01 14:20:36.368324,Y,U,1.03238296509
>> 2011-04-01 14:20:35.342732,Y,C,0.0252721309662
>> 2011-04-01 14:20:34.337209,Y,R,0.00522899627686
>>
>
> See ?strptime:
>
>  Specific to R is %OSn, which for output gives the seconds to 0 <= n <= 6
>  decimal places (and if %OS is not followed by a digit, it uses the setting of
>  getOption("digits.secs"), or if that is unset, n = 3). Further, for strptime
>  %OS will input seconds including fractional seconds. Note that %S ignores
>  (and not rounds) fractional parts on output.
>
>
> dat <- read.table(textConnection(
> '"Time","Include","Kind","Duration"
> 2011-04-01 14:20:36.368324,Y,U,1.03238296509
> 2011-04-01 14:20:35.342732,Y,C,0.0252721309662
> 2011-04-01 14:20:34.337209,Y,R,0.00522899627686'),
> header=TRUE, sep=",")
>
> R> dat$Time <- as.POSIXct(dat$Time, "%Y-%m-%d %H:%M:%OS6")
> R> dat$Time
> [1] "2011-04-01 14:20:36.368" "2011-04-01 14:20:35.343"
> [3] "2011-04-01 14:20:34.337"
> R> options(digits.secs=6)
> R> dat$Time
> [1] "2011-04-01 14:20:36.368324" "2011-04-01 14:20:35.342732"
> [3] "2011-04-01 14:20:34.337209"
> R> class(dat$Time)
> [1] "POSIXct" "POSIXt"
>
> HTH,
> Rainer
>
> ______________________________________________
> R-help@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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
R-help@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.

Reply via email to