[R] synchronisation of time series data using interpolation

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 25 13:57:09 CEST 2009


Create the series as zoo series from the data, and then merge them and
fill in NAs with interpolated values using na.approx.  Finally use
window to pick off the times that were in z1 and plot.  See the three
vignettes that come with zoo and for time and dates see the article in
R News 4/1 and its references.

Lines1 <- "time,datum
01:00:00,500
01:00:15,600
01:00:30,750
01:00:45,720
01:01:00,700
01:01:15,725
01:01:30,640
01:01:45,710"

Lines2 <- "time,datum
01:00:12,20
01:01:01,55
01:01:55,22"

library(zoo)
library(chron)

z1 <- read.zoo(textConnection(Lines1), header = TRUE, sep = ",", FUN = times)
z2 <- read.zoo(textConnection(Lines2), header = TRUE, sep = ",", FUN = times)

z3 <- window(na.approx(merge(z1, z2)), time(z1))
plot(z3$z1, z3$z2)


On Fri, Sep 25, 2009 at 1:41 AM, e-letter <inpost at gmail.com> wrote:
> Readers,
>
> I have data with different time stamps that I wish to plot (for example):
>
> data set 1
> time(hh:mm:ss),datum
> 01:00:00,500
> 01:00:15,600
> 01:00:30,750
> 01:00:45,720
> 01:01:00,700
> 01:01:15,725
> 01:01:30,640
> 01:01:45,710
> data set 2
> time,datum
> 01:00:12,20
> 01:01:01,55
> 01:01:55,22
>
> The time interval in data set 1 does not change, but the time interval
> in data set 2 does change, such that for a specific total time range
> (e.g. 60 minutes) there will be more data in data set 1 than in data
> set 2.
>
> I thought I could solve this problem using interpolation, to create a
> new data set using data from data set 2, interpolated to the time
> stamps in data set 1:
>
> data set 3
> time,datum
> 01:00:00,18
> 01:00:15,23
> 01:00:30,30
> 01:00:45,41
> 01:01:00,53
> 01:01:15,46
> 01:01:30,38
> 01:01:45,29
>
> Therefore I would then be able to plot the data in data set 1 against
> the interpolated data in data set 3, because there would be equal
> quantities of data in both data sets. I've looked at the interp
> function in the help manual, but I don't understand if this function
> can perform the task I want. Any advice please?
>
> Yours,
>
> rhelp at conference.jabber.org
> r 251 (27-06-07)
> mandriva 2008
>
> ______________________________________________
> 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