[R-SIG-Finance] how to fill-in time stamp

Gabor Grothendieck ggrothendieck at gmail.com
Mon Sep 6 20:59:45 CEST 2010


On Mon, Sep 6, 2010 at 2: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

Below we have three possibilities depending on what "fill in" means.  The first
fills in values with the last value present, the second does linear
interpolation
and the last just inserts NAs.  See zoo FAQ #13 and ?na.locf and ?na.approx
in zoo.

Lines <- "timestamp,time-step-index,value
2009-11-23 15:58:21,23301,800
2009-11-23 15:58:29,23309,950"

library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE, sep = ",", tz = "")

# create a one second grid
g <- seq(start(z), end(z), by = "sec")

# three alternatives
na.locf(z, xout = g)
na.approx(z, xout = g)
merge(z, zoo(, g))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-SIG-Finance mailing list