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

Jeffrey Ryan jeffrey.ryan at lemnica.com
Sat Jun 30 20:51:28 CEST 2012


Or you could simply change the index explicitly:

> indexFormat(ICSA) <- "%a"
> head(ICSA)
      ICSA
Sat 208000
Sat 207000
Sat 217000
Sat 204000
Sat 216000
Sat 229000
> index(ICSA) <- as.Date(index(ICSA))-1
> head(ICSA)
      ICSA
Fri 208000
Fri 207000
Fri 217000
Fri 204000
Fri 216000
Fri 229000
> indexFormat(ICSA) <- NULL  # use the default
> head(ICSA)
             ICSA
1967-01-06 208000
1967-01-13 207000
1967-01-20 217000
1967-01-27 204000
1967-02-03 216000
1967-02-10 229000
>

HTH
Jeff



On Sat, Jun 30, 2012 at 1:39 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> 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
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



-- 
Jeffrey Ryan
jeffrey.ryan at lemnica.com

www.lemnica.com
www.esotericR.com



More information about the R-SIG-Finance mailing list