Skip to content
Prev 8658 / 15274 Next

AsOf join in R

On Tue, 2011-10-04 at 23:41 -0400, Robert A'gata wrote:
A <- xts(c(10,15,20,25),
order.by=as.POSIXct(c("2011-09-01","2011-09-09","2011-09-10","2011-09-15")))
B <- xts(c(1.1,1.5,1.3,1.7),
order.by=as.POSIXct(c("2011-08-31","2011-09-09","2011-09-11","2011-09-12")))
AsOf<-function(a,b) {
   x<-cbind(a,b) 
   x[,2]<-na.locf(x[,2]) 
   x[!is.na(x[,1])] 
}
AsOf(A,B)

#####################
#           ..1 ..2
#2011-09-01  10 1.1
#2011-09-09  15 1.5
#2011-09-10  20 1.5
#2011-09-15  25 1.7
#####################