Re: [R] Zoo series to a date time stamp that is regular

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 28 Jun 2010 17:10:52 -0400

On Mon, Jun 28, 2010 at 4:42 PM, stephen sefick <ssefick_at_gmail.com> wrote:
> NOTE: I will provide data if necessary, but I didn't want clutter
> everyones mailbox
>
> All:
> I have a time series with level and temperature data for 11 sites for
> each of three bases.  I will have to do this more than once is what I
> am saying here.  OK,  The time series are zoo objects with index
> values in chron format.  The problem is that the date and times should
> be at even 15 min intervals, but because of operator error (me) they
> are on 15 min intervals for some of the data and on 15+1 min intervals
> for some and 15+1.56 for some of the same site.  I would like to
> create a regular 15min time series from this and need to know if I can
> just create an empty time series with the entire date time I would
> like and then aggregate.  I am not sure how merge or aggregate would
> act in this particular situation.  I want to "snap" readings to the
> nearest 15min interval.
> kindest regards,

In the development version of zoo na.locf has an xout argument (modeled after ?approx). If you add an xout = g argument where g is the desired grid then it will fill the grid with NAs, fill the NAs in the usual na.locf way and then drop any points not on the grid. Here is an example taken the development version of the zoo faq which uses 10 minutes. Its just two lines. The first sets up the grid and the second is the just-mentioned na.locf call.

# load development version of na.locf
library(zoo)
library(chron)
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/na.locf.R?revision=725&root=zoo")

# read data into z

Lines <- "Time,Value
2009-10-09 5:00:00,210
2009-10-09 5:05:00,207
2009-10-09 5:17:00,250
2009-10-09 5:30:00,193
2009-10-09 5:41:00,205
2009-10-09 6:00:00,185"

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

# create 10 minute grid, g, and align to it g <- seq(start(z), end(z), by = times("00:10:00")) na.locf(z, xout = g)

At the expense of a slightly more complex call, its also possible to calculate it with the current CRAN version of zoo if you use na.approx in place of the na.locf line above as shown:

na.approx(z, xout = g, method = "constant", rule = 2)



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. Received on Mon 28 Jun 2010 - 21:15:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jun 2010 - 22:10:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive