[R] time series manipulation what functions are "best"

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 17 02:29:28 CET 2012


On Thu, Feb 16, 2012 at 7:31 PM, Henry <hccoles at lbl.gov> wrote:
> Newbie question - mechanical engineer trying to learn R
> I've had success with plotting time series data and even made a heat map
> using R Graphs Cookbook by Mittal.
> I have a new problem - I need to align a number of time series data
> "columns" to the desired regular exact time stamp vector. The target time
> vector could be finer or more course than the typical time stamp spacing
> found.  Probably the two time stamp spacings I will want is 1 minute or 5
> minutes.  Then I need to look at the average for 15 minute blocks of time.
> This is likely a bit different problem.
> Should I look at zoo functions, I installed the zoo package or look at
> approx, approxfun, spline, splinefun?  Zoo seemed to come up most frequently
> searching here and with Google.
>
> I think I want a curve fit type (I think there are multiple types)
> interpolation not just straight line.
>
> Here is a sample of my data:
>
> I can get the time all in one "box" in csv format.  I've used as.POSIXlt
> previous to this with success.  Zoo functions seem to also have time
> functions, so that is a bit confusing.
>
>
> 10/11/2011 23:30:01     432.22
> 10/11/2011 23:31:17     432.32
> 10/11/2011 23:35:00     432.32
> 10/11/2011 23:36:18     432.22
> 10/11/2011 23:37:18     432.72
> 10/11/2011 23:39:19     432.23
> 10/11/2011 23:40:02     432.23
> 10/11/2011 23:45:00     432.23
> 10/11/2011 23:45:20     429.75
> 10/11/2011 23:46:20     429.65
> 10/11/2011 23:50:00     429.65
> 10/11/2011 23:51:22     429.75
> 10/11/2011 23:55:01     429.75
> 10/11/2011 23:56:23     429.55
> 10/12/2011 0:00:07      429.55
> 10/12/2011 0:01:24      429.95
> 10/12/2011 0:05:00      429.95
> 10/12/2011 0:06:25      429.85
> 10/12/2011 0:10:00      429.85
> 10/12/2011 0:11:26      428.85
> 10/12/2011 0:15:00      428.85
> 10/12/2011 0:20:03      428.85
> 10/12/2011 0:21:29      428.75
> 10/12/2011 0:25:01      428.75
> 10/12/2011 0:30:01      428.75
> 10/12/2011 0:31:31      428.75
>

If you are using zoo then the zoo FAQ discusses grids
   http://cran.r-project.org/web/packages/zoo/index.html
and the other 4 vignettes (pdf documents) and reference manual on that
page discuss more.

zoo does not supply its own time classes except where classes are
elsewhere missing.   Its design is completely independent of the time
class and it works with any time class that supports certain methods
(and that includes all popular ones).  See R News 4/1 for more on date
and time classes.

Here is some code:

Lines <- "10/11/2011 23:30:01     432.22
10/11/2011 23:31:17     432.32
10/11/2011 23:35:00     432.32
10/11/2011 23:36:18     432.22
10/11/2011 23:37:18     432.72
10/11/2011 23:39:19     432.23
10/11/2011 23:40:02     432.23
10/11/2011 23:45:00     432.23
10/11/2011 23:45:20     429.75
10/11/2011 23:46:20     429.65
10/11/2011 23:50:00     429.65
10/11/2011 23:51:22     429.75
10/11/2011 23:55:01     429.75
10/11/2011 23:56:23     429.55
10/12/2011 0:00:07      429.55
10/12/2011 0:01:24      429.95
10/12/2011 0:05:00      429.95
10/12/2011 0:06:25      429.85
10/12/2011 0:10:00      429.85
10/12/2011 0:11:26      428.85
10/12/2011 0:15:00      428.85
10/12/2011 0:20:03      428.85
10/12/2011 0:21:29      428.75
10/12/2011 0:25:01      428.75
10/12/2011 0:30:01      428.75
10/12/2011 0:31:31      428.75"

library(zoo)
library(chron)

fmt <- "%m/%d/%Y %H:%M:%S"
toChron <- function(d, t) as.chron(paste(d, t), format = fmt)

z <- read.zoo(text = Lines, index = 1:2, FUN = toChron)

# 5 minute aggregates
m5 <- times("00:05:00")
ag5 <- aggregate(z, trunc(time(z), m5), mean)

# 5 minute spline fit
g <- seq(trunc(start(z), m5), end(z), by = m5)
na.spline(z, xout = g)

# 5 minute linear approx
na.approx(z, xout = g)

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



More information about the R-help mailing list