[R-SIG-Finance] [Q] How to omit NA values in TTR package + Jeff Cooper's 5 day MOM
Brian G. Peterson
brian at braverock.com
Tue Mar 3 20:10:44 CET 2015
On 03/03/2015 12:55 PM, Alex Badoi wrote:
> 1. NA Values are making my code useless as it stops half way through. I
> need to run this on a few hundred stocks. It works perfectly with no NA
> values. I tried to use na.omit and na.rm=T.
use try() in your MOM function, and return either an answer or an empty
result.
> Alternatively i am looking for a way through which i could search through
> my stockData environment for vectors containing NA values and remove these
> vectors entirely before running it through the following code.
These aren't vectors, they're xts objects.
for a given xts object x
any(is.na(x))
will return TRUE or FALSE
So you could loop through the list something like this:
for (symbol in ls(pos=stockData)){
if(any(is.na(stockData[[symbol]])) rm(symbol,pos=stockData)
}
but it seems that try() and making your MOM function more robust is
likely to be a better answer.
Regards,
Brian
--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
More information about the R-SIG-Finance
mailing list