Haroon Lone
Haroon Lone

Reputation: 2949

Addition over zoo/xts objects in R

I have two xts objects of different lengths in R, defined as:

library(xts)
seq <- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-02"), by = "1 hour")
ob1 <- xts(data.frame(val=1:(length(seq))),seq)
seq2 <- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-03"), by = "1 hour")
ob2 <- xts(data.frame(val=1:(length(seq2))),seq2)

Now when I perform addition over ob1 and ob2 as

ob1 + ob2

I get the result, but the addition is done over the timestamps which are common in both. Is there a way where I can retain all the distinct timestamps of both the objects and still get the result?

Upvotes: 0

Views: 117

Answers (1)

Buggy
Buggy

Reputation: 2100

Not the prettiest solution, but from the top of my head...

test<-merge(ob1,ob2)
test<-xts(rowSums(test, na.rm=T), order.by = time(test))

Upvotes: 0

Related Questions