title: Downloading Yahoo stock prices in R
body: <p>This is a newbie question in R. I am downloading yahoo finance monthly stock price data  using R where the ticker names are read from a text file. I am using a loop to read the ticker names to download the data  and putting them in a list. My problem is some ticker names may not be correct thus my code stops when it encounters this case. I want the following.</p>
  
  <ol>
  <li>skip the ticker name if it is not correct.</li>
  <li>Each element in the list is a dataframe. I want the ticker names to be appended to variable names in element dataframes.</li>
  <li>I need an efficient way to create a dataframe that has the closing prices as variables.</li>
  </ol>
  
  <p>Here is the sample code for the simplified version of my problem.</p>
  
  <pre><code>library(tseries)  
  tckk &lt;- c("MSFT", "C", "VIA/B", "MMM") # ticker names defined  
  numtk &lt;- length(tckk);  
  ustart &lt;- "2000-12-30";
  uend &lt;- "2007-12-30" # start and end date  
  all_dat &lt;- list(); # empty list to fill in the data  
  for(i in 1:numtk)  
  {  
    all_dat[[i]] &lt;- xxx &lt;- get.hist.quote(instrument = tckk[i], start=ustart, end=uend, quote = c("Open", "High", "Low", "Close"), provider = "yahoo", compression = "m")  
  }   
  </code></pre>
  
  <p>The code stops at the third entry but I want to skip this ticker and move on to "MMM". I have heard about Trycatch() function but do not know how to use it.  </p>
  
  <p>As per question 2, I want the variable names for the first element of the list to be "MSFTopen", "MSFThigh", "MSFTlow", and "MSFTclose". Is there a better to way to do it apart from using a combination of loop and paste() function.  </p>
  
  <p>Finally, for question 3, I need a dataframe with three columns corresponding to closing prices. Again, I am trying to avoid a loop here.</p>
  
  <p>Thank you.</p>