On Fri, Jan 4, 2013 at 5:53 PM, Matthijs Daelman <matthijs.dael...@gmail.com> wrote: > Hi > > I have to time series with a different time base. > > The first has only sporadic datapoints: > 2011-02-01 15.29130 > 2011-02-08 17.60278 > 2011-02-15 17.99737 > 2011-02-22 25.43690 > > The other has a daily datapoint: > 2011-02-01 342.34 > 2011-02-02 68.45 > 2011-02-03 130.47 > 2011-02-04 129.86 > 2011-02-05 81.98 > 2011-02-06 77.30 > 2011-02-07 81.38 > 2011-02-08 139.95 > 2011-02-09 124.40 > ...etc. > > In Excel, it is fairly easy to get these two time series in one and the > same scatter plot, with two Y-axes, but how would you do this with R? > > Also, I would like to obtain a data frame that contains both variables: > 2011-02-01 342.34 15.29130 > 2011-02-02 68.45 NA > 2011-02-03 130.47 NA > 2011-02-04 129.86 NA > 2011-02-05 81.98 NA > 2011-02-06 77.30 NA > 2011-02-07 81.38 NA > 2011-02-08 139.95 17.60278 > 2011-02-09 124.40 NA > etc. > > I can imagine that the data frame can be obtained using (nested) loops, but > that does not seem very efficient with a large dataset. >
Try this: data1 <- "2011-02-01 15.29130 2011-02-08 17.60278 2011-02-15 17.99737 2011-02-22 25.43690" data2 <- "2011-02-01 342.34 2011-02-02 68.45 2011-02-03 130.47 2011-02-04 129.86 2011-02-05 81.98 2011-02-06 77.30 2011-02-07 81.38 2011-02-08 139.95 2011-02-09 124.40" library(zoo) # read into zoo objects z1 <- read.zoo(text = data1) z2 <- read.zoo(text = data2) # set up plot(na.aggregate(merge(z1, zoo(, time(z2)))), type = "n", ylab = "") # plot first series lines(z1) # plot 2nd series in red with red Y axis par(new = TRUE) plot(z2, col = "red", xaxt = "n", yaxt = "n", ylab = "") axis(4, col = "red") See ?plot.zoo for more examples. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-help@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.