[R] Calculate Timeseries Interval

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 16 17:27:27 CEST 2009


Try this:

> Lines <- "19:21:59.855
+  19:21:59.905
+  19:21:59.955
+  19:22:0.5
+  19:22:0.55
+  19:22:0.105
+  19:22:0.155
+  19:22:0.205
+  19:22:0.255
+  19:22:0.305
+  19:22:0.355
+  19:22:0.405"
>
> DF <- read.table(textConnection(Lines))
> library(chron)
> tt <- times(DF[[1]])
> diff(tt)
 [1]  5.787037e-07  5.787037e-07  6.307870e-06  5.787037e-07 -5.150463e-06
 [6]  5.787037e-07  5.787037e-07  5.787037e-07  5.787037e-07  5.787037e-07
[11]  5.787037e-07


On Thu, Jul 16, 2009 at 11:18 AM, Dave Deriso<dderiso at ucsd.edu> wrote:
> Hello,
>
> I have been attempting to write a script that automatically calculates time
> intervals from a list of hh.mm.ss.ms timestamps. Should be easy, but I keep
> running into problems. Any help would be greatly appreciated!!
>
> Problems:
> 1. If I parse the timestamp, it doesn't always return the digits after the
> decimal
> 2. If I subtract .5 - .955 (as in 19:22:0.5 - 19:21:59.955), then I get a
> really big negative number
>
> Here is sample data:
>
>  19:21:59.855
>  19:21:59.905
>  19:21:59.955
>  19:22:0.5
>  19:22:0.55
>  19:22:0.105
>  19:22:0.155
>  19:22:0.205
>  19:22:0.255
>  19:22:0.305
>  19:22:0.355
>  19:22:0.405
>
> Here is my code:
>
> #attempts to parse milliseconds from timestamp
> for (i in 1:200){k[i] =
> rbind(substring(TimeStamp[i,3],8:10)[which(substring(TimeStamp[i,3],8:10)<1)])}
>
> #attempts to intelligently subtract intervals
> for (i in 1:200){if(as.numeric(k[(i+1)])>as.numeric(k[(i)])){k2[i] =
> rbind(as.numeric(k[(i+1)])-as.numeric(k[(i)]))}else{k2[i] =
> rbind((1+as.numeric(k[(i+1)]))-as.numeric(k[(i)]))}}
>
> Thanks so much for your help!!!
>
> Best,
>
> Dave Deriso
> UCSD Psychiatry
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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