[R-SIG-Finance] zoo: how to find for series x closest day in series y?
Matthieu Stigler
matthieu.stigler at gmail.com
Fri Apr 23 11:10:19 CEST 2010
Hi
I have two series, x and y, which don't have the same index. I then
need to find which in y is the closest day in x. How can do I do this in
a clean way? I have here a dirty for loop solution...
library(zoo)
#my series
x<-zoo(1:3,as.Date(c("1992-12-13", "1997-05-12", "1997-07-13")))
y<-zoo(1:5,as.Date(c("1992-12-15", "1992-12-16",
"1997-05-10","1997-05-19", "1997-07-13")))
#but index is not always the same:
index(x)%in%index(y)
#my (dirty?) solution
xnew<-x
for(i in which(!index(x)%in%index(y)))
xnew[i]<-which.min(abs(as.numeric(index(x)[i]-index(y))))
xnew
Is there something cleaner one could use to find the closest available day?
Thanks a lot!
Matthieu
More information about the R-SIG-Finance
mailing list