xts object – subscript out of bounds
[This article was first published on Copula, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I bet you have seen this error a few times. When I compare large xts objects with different number of observations it would hit me right towards the end of the analysis.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I wrote a small function, which allows me to check the length of the sets in advance and retains only the symbols, which hold the exact number of observations required.
benchmark is simply my ticker with the number of observations I would like to see.
checkrow <- function(Tickers,Bench,From,To)
{
getSymbols(Bench, index.class=”POSIXct”, from = From, to = To,src=”yahoo”)
my = NA
y=get(benchmark)
m=NROW(y)
for(sticker in tickers){
x=get(sticker)
if( NROW(x)
{
removeSymbols(sticker)
}
if( NROW(x)==m)
{
a=NROW(my)
my[a+1]=sticker
}
}
my = my[-1]
tickers = my
return(tickers)
}
To leave a comment for the author, please follow the link and comment on their blog: Copula.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.