Question

Consider the following zoo objects:

 x <- data.frame(Date = seq(as.Date("2013-02-01"), length=6, by="1 month") - 1, x = seq(6))
 y <- data.frame(Date = seq(as.Date("2013-02-01"), length=4, by="1 month"), y = seq(4))
 z <- data.frame(Date = seq(as.Date("2012-11-01"), length=9, by="1 month"), z = seq(9))

 x.zoo = zoo(x[,-1], order.by = x[,1])
 y.zoo = zoo(y[,-1], order.by = y[,1])
 z.zoo = zoo(z[,-1], order.by = z[,1])

Note that the 3 zoo objects are of different lenghts. I would like to align all the objects with respect to the date of variable "x": in other words, I want to create a new dataframe that contains a Date column (index of object x), and fill for each variable with the closest available observation.

Here is the result I am looking for:

         Date x  y  z
 1 2013-01-31 1  1  4
 2 2013-02-28 2  2  5
 3 2013-03-31 3  3  6
 4 2013-04-30 4  4 NA
 5 2013-05-31 5 NA NA
 6 2013-06-30 6 NA NA

Note that, in reality, I have 10 variables that I want to align with x. I am aware of the function na.locf from the zoo package, but I am not sure how to adapt it in the context of multiple zoo objects.

Is there anyway I could do that efficiently?

Thank you!

Was it helpful?

Solution

Try this:

xx.zoo <- aggregate(x.zoo, as.yearmon)
yy.zoo <- aggregate(y.zoo, as.yearmon)
zz.zoo <- aggregate(z.zoo, as.yearmon)
out <- merge(x = xx.zoo, y = yy.zoo, z = zz.zoo, all = c(TRUE, FALSE, FALSE))

or if you don't want to write it out:

L <- setNames(list(x.zoo, y.zoo, z.zoo), c("x", "y", "z"))
all. <- c(TRUE, rep(FALSE, length(L) - 1))  # all FALSE except first
out <- do.call(merge, c(lapply(L, aggregate, as.yearmon), all = list(all.)))

which gives:

> out
         x  y z
Jan 2013 1 NA 3
Feb 2013 2  1 4
Mar 2013 3  2 5
Apr 2013 4  3 6
May 2013 5  4 1
Jun 2013 6 NA 2

The yearmon times may be preferable but if its important to you that the times be the same as the x.zoo times then add this:

time(out) <- time(x.zoo)

UPDATE: Minor improvements.

OTHER TIPS

An alternative, using @Ricardo Saporta's suggestion of round_date:

library(lubridate)

# round dates in x.zoo to nearest month
index(x.zoo) <- round_date(index(x.zoo), "month")

# merge
xyz <- merge(x = x.zoo, y = y.zoo, z = z.zoo, all = c(TRUE, FALSE, FALSE))

# make data frame from original x dates and data from zoo object
data.frame(Date = x$Date, coredata(xyz))

#         Date x  y z
# 1 2013-01-31 1  1 4
# 2 2013-02-28 2  2 5
# 3 2013-03-31 3  3 6
# 4 2013-04-30 4  4 7
# 5 2013-05-31 5 NA 8
# 6 2013-06-30 6 NA 9
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top