R365: Day 36- {xts}

So as we saw in the previous post, {zoo} is a useful and powerful tool for handling and analyzing time series data. Unfortunately, the datasets that I am working with are 1) irregularly spaced and  2) irregularly timed. This means that some of the ordinary things that I could do with time series data (lags, ACFs, CCFs, etc) I cannot do without some manipulation and coercion. The R package {xts} can help in some ways by allowing you to merge two time series datasets without lining them up or organizing them before hand. This article gave me the idea to use {xts}, as it answered my problem of how to plot things with different time series. Therefore I will stea- er… borrow their example (with a few modifications) to see how this package and function work.

##set up the dates for the example system
date.a<-seq(as.Date('2014-01-01'),as.Date('2014-02-01'),by = 2)
date.b<-seq(as.Date('2014-01-01'),as.Date('2014-02-15'),by = 3)
date.c<-seq(as.Date('2014-01-01'),as.Date('2014-02-01'),by = 1)
##create data frames for the time series
df.a <- data.frame(time=date.a, A=sin((1:16)*pi/8))
df.b <- data.frame(time=date.b, B=cos((1:16)*pi/8))
df.c <- data.frame(time=date.c, C=cos((1:16)*pi/16))
##Merge the time series as xts() time series using merge()
my.ts <- merge(xts(df.a$A,df.a$time),xts(df.b$B,df.b$time),xts(df.c$C,df.c$time))
##look at what the time series looks like
##treat my.ts as a zoo object, and use na.approx to replace NAs with interpolated values
z <- na.approx(as.zoo(my.ts))
# plot.zoo
plot(z, screen = 1, col = 1:3, ylab = "Y", xlab = "")
# xyplot.zoo
xyplot(z, screen = 1, col = 1:3, ylab = "Y", xlab = "")
# autoplot.zoo
autoplot(z, facet = NULL) + ylab("Y")

You even get to pick your poison as far as {lattice}, {ggplot2}, or {base} is concerned


merge() is a really cool and powerful data organization function in R {zoo} that lets you merge data points along a common time series. I know that I have spent good chunks of time organizing things in Excel that I probably could have done in five minutes in {zoo}.

In the above example, we chose to interpolate between NA values using the na.approx() function. We can also do other things with NA values, such as omit them (using {stats} na.omit()), aggregate them (na.aggregate() from {zoo}), and trim off some of the values (na.trim from {zoo}). Beware that some of these can have consequences! For instance, na.omit omits rows that have ANY NAs, so the resulting graph looks like this:


because two of our time series did not have dates after the 31st! Bummer.

What happens if you don’t address the NAs and you just leave it as an as.zoo() object?


Also a bummer. It looks like you have to address the NAs somehow, but I have no good ideas for those of you who are squeemish about aggregating or reducing data outside of “just plot the points idiot”.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s