[R-SIG-Finance] aligning dates from different sources

Joshua Ulrich josh.m.ulrich at gmail.com
Sat Jun 30 20:39:58 CEST 2012


On Sat, Jun 30, 2012 at 1:21 PM, Eric Thungstom
<eric.thungstrom at gmail.com> wrote:
> I've downloaded some FRED data (ICSA) using quantmod and I'm trying to
> align it with weekly GSPC data. I'm running into trouble because the ICSA
> data seems to come out on Saturdays and weekly closes of GSPC are usually
> on Friday. I tried a simple fix of subtracting 1 from the ICSA dates but I
> still run into problems around holidays.  For example, the market was
> closed on April 6, 2012 so I can't align the data for that week.
>
> Any suggestions how I get the dates lined up so I can do some further
> analysis ?  I know I could use a brute force method of doing a simple cbind
> (combined <-cbind(sp,ICSA[,1]))  instead of merge but wondering if there's
> a better way that takes holidays into account.
>
cbind.xts simply calls merge.xts, so I'm not sure why you would expect
different results.

You could merge the two raw series, use na.locf to fill in the missing
values, then use apply.weekly to get the last value for each week:

x <- merge(ICSA,Ad(GSPC))
x <- na.locf(x)
y <- apply.weekly(x, last)

> require(quantmod)
> getSymbols('ICSA', src='FRED')
> getSymbols('^GSPC', from='1967-01-01', to=Sys.Date())
> index(ICSA) <-index(ICSA)-1
> sp <-to.weekly(GSPC)[,6]
> names(sp) <-'GSPC'
> combined <-merge(ICSA,sp)
> tail(combined, 15)
<snip>

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



More information about the R-SIG-Finance mailing list