xts object – subscript out of bounds

February 6, 2013
By

(This article was first published on Copula, and kindly contributed to R-bloggers)

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.
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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)