[R] Math problem with xts objects
Joshua Ulrich
josh.m.ulrich at gmail.com
Tue May 14 03:12:54 CEST 2013
On Mon, May 13, 2013 at 6:47 PM, Noah Silverman <noahsilverman at ucla.edu> wrote:
> Hello,
>
> I coming across a strange problem doing math on an xts object.
>
> If I have an xts object of stock prices (perhaps 5 minute bars of open, high, low,close) and want to do some math, the results fail.
>
> For example:
>
> d$close[10] - d$open[10] works perfectly
>
> d$close[10] - d$open[9] fails. I just get an answer of "numeric(0) Index: numeric(0)".
>
> My guess is that xts is breaking because the two items in my equation have different time stamps. However, the actual data is correct, and the xts matrix looks correct.
>
Nothing is broken or strange. xts' behavior is consistent with zoo,
ts, etc. What's strange is expecting a time-series class to do
operations independent of time.
> Note: It works if I do things the loooong way around.
> as.numeric(d$close[10]) - as.numeric( d$open[9] )
>
> But, that is horrendously slow.
>
> Suggestions?
>
Use lag.
Best,
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
R/Finance 2013: Applied Finance with R | www.RinFinance.com
More information about the R-help
mailing list