[R-SIG-Finance] how to fill-in time stamp
Jeff Ryan
jeff.a.ryan at gmail.com
Mon Sep 6 20:47:05 CEST 2010
Hi tsf (first name??)
Using xts or zoo, you can create a time object with the times you
"want", and merge the two to get the result you intend. With xts this
will be very efficient. You can then convert into timeSeries if
needed. Of course timeSeries has some ability to do this as well.
> library(xts)
> x <- xts(1:20, Sys.time()+1:20)
> x
[,1]
2010-09-06 13:46:06 1
2010-09-06 13:46:07 2
2010-09-06 13:46:08 3
2010-09-06 13:46:09 4
2010-09-06 13:46:10 5
2010-09-06 13:46:11 6
2010-09-06 13:46:12 7
2010-09-06 13:46:13 8
2010-09-06 13:46:14 9
2010-09-06 13:46:15 10
2010-09-06 13:46:16 11
2010-09-06 13:46:17 12
2010-09-06 13:46:18 13
2010-09-06 13:46:19 14
2010-09-06 13:46:20 15
2010-09-06 13:46:21 16
2010-09-06 13:46:22 17
2010-09-06 13:46:23 18
2010-09-06 13:46:24 19
2010-09-06 13:46:25 20
> y <- x[c(1,2,3,10,19)]
> y
[,1]
2010-09-06 13:46:06 1
2010-09-06 13:46:07 2
2010-09-06 13:46:08 3
2010-09-06 13:46:15 10
2010-09-06 13:46:24 19
> merge(y, time(x))
y
2010-09-06 13:46:06 1
2010-09-06 13:46:07 2
2010-09-06 13:46:08 3
2010-09-06 13:46:09 NA
2010-09-06 13:46:10 NA
2010-09-06 13:46:11 NA
2010-09-06 13:46:12 NA
2010-09-06 13:46:13 NA
2010-09-06 13:46:14 NA
2010-09-06 13:46:15 10
2010-09-06 13:46:16 NA
2010-09-06 13:46:17 NA
2010-09-06 13:46:18 NA
2010-09-06 13:46:19 NA
2010-09-06 13:46:20 NA
2010-09-06 13:46:21 NA
2010-09-06 13:46:22 NA
2010-09-06 13:46:23 NA
2010-09-06 13:46:24 19
2010-09-06 13:46:25 NA
> merge(y, time(x), fill=na.locf)
y
2010-09-06 13:46:06 1
2010-09-06 13:46:07 2
2010-09-06 13:46:08 3
2010-09-06 13:46:09 3
2010-09-06 13:46:10 3
2010-09-06 13:46:11 3
2010-09-06 13:46:12 3
2010-09-06 13:46:13 3
2010-09-06 13:46:14 3
2010-09-06 13:46:15 10
2010-09-06 13:46:16 10
2010-09-06 13:46:17 10
2010-09-06 13:46:18 10
2010-09-06 13:46:19 10
2010-09-06 13:46:20 10
2010-09-06 13:46:21 10
2010-09-06 13:46:22 10
2010-09-06 13:46:23 10
2010-09-06 13:46:24 19
2010-09-06 13:46:25 19
>
HTH
Jeff
On Mon, Sep 6, 2010 at 1:37 PM, t sf <tsf2001 at gmail.com> wrote:
> Hi list,
>
> Using R and possibly Rmetrics packages, what is the best way to fill-in
> missing time stamp in a time series? For example, I have
> some data like the following:
>
> timestamp time-step-index value
> 2009-11-23 15:58:21 23301 800
> 2009-11-23 15:58:29 23309 950
>
> There is a gap of 8 seconds between 15:58:21 and 15:58:29 . I want to
> fill-in the missing time stamp for these seconds.
>
> Current I am using a loop to do this, but it seems to be not very efficient
> :
>
> start=time(someData[1,]);
> numSec = 30000;
> for (k in 1:numSec ) {
> timeTemplate[k,1]=as.character(start + k, format="%Y%m%d%H%M%S");
> }
> where someData is a timeSeries object.
>
> -tsf
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>
--
Jeffrey Ryan
jeff.a.ryan at gmail.com
More information about the R-SIG-Finance
mailing list