title: yahoo tickers, time zone, and merging
body: <p>I would like to download daily data from yahoo for the S&amp;P 500, the DJIA, and 30-year T-Bonds, map the data to the proper time zone, and merge them with my own data. I have several questions.</p>
  
  <ol>
  <li><p>My first problem is getting the tickers right. From yahoo's website, it looks like the tickers are: ^GSPC, ^DJI, and ^TYX. However, ^DJI fails. Any idea why?</p></li>
  <li><p>My second problem is that I would like to constrain the time zone to GMT (I would like to ensure that all my data is on the same clock, GMT seems like a neutral choice), but I couldn' get it to work.</p></li>
  <li><p>My third problem is that I would like to merge the yahoo data with my own data, obtained by other means and available in a different format. It is also daily data.</p></li>
  </ol>
  
  <p>Here is my attempt at constraining the data to the GMT time zone. Executed at the top of my R script.</p>
  
  <pre><code>Sys.setenv(TZ = "GMT")
  # &gt; Sys.getenv("TZ")
  # [1] "GMT"
  # the TZ variable is properly set
  # but does not affect the time zone in zoo objects, why?
  </code></pre>
  
  <p>Here is my code to get the yahoo data:</p>
  
  <pre><code>library("tseries")
  library("xts")
  
  date.start &lt;- "1999-12-31"
  date.end &lt;- "2013-01-01"
  
  # tickers &lt;- c("GSPC","TYX","DJI")
  # DJI Fails, why?
  # http://finance.yahoo.com/q?s=%5EDJI
  tickers &lt;- c("GSPC","TYX") # proceed without DJI
  
  z &lt;- zoo()
  index(z) &lt;- as.Date(format(time(z)),tz="")
  
  for ( i in 1:length(tickers) ) 
    { 
       cat("Downloading ", i, " out of ", length(tickers) , "\n")
       x &lt;- try(get.hist.quote(
           instrument = paste0("^",tickers[i])
           , start = date.start
           , end = date.end
           , quote = "AdjClose"
           , provider = "yahoo"
           , origin = "1970-01-01"
           , compression = "d"
           , retclass = "zoo" 
           , quiet = FALSE )
         , silent = FALSE )
       print(x[1:4]) # check that it's not empty
       colnames(x) &lt;- tickers[i]
       z &lt;- try( merge(z,x), silent = TRUE )
  }
  </code></pre>
  
  <p>Here is the dput(head(df)) of my dataset:</p>
  
  <pre><code>df &lt;- structure(list(A = c(-0.011489000171423, -0.00020300000323914, 
  0.0430639982223511, 0.0201549995690584, 0.0372899994254112, -0.0183669999241829
  ), B = c(0.00110999995376915, -0.000153000000864267, 0.0497750006616116, 
  0.0337960012257099, 0.014121999964118, 0.0127800004556775), date = c(9861, 
  9862, 9863, 9866, 9867, 9868)), .Names = c("A", "B", "date"
  ), row.names = c("0001-01-01", "0002-01-01", "0003-01-01", "0004-01-01", 
  "0005-01-01", "0006-01-01"), class = "data.frame")
  </code></pre>
  
  <p>I'd like to merge the data in df with the data in z. I can't seem to get it to work.</p>
  
  <p>I am new to R and very much open to your advice about efficiency, best practice, etc.. Thanks.</p>
  
  <p><strong>EDIT: SOLUTIONS</strong></p>
  
  <ol>
  <li><p>On the first problem: following GSee's suggestions, the Dow Jones Industrial Average data may be downloaded with the quantmod package: thus, instead of the "^DJI" ticker, which is no longer available from yahoo, use the "DJIA" ticker. Note that there is no caret in the "DJIA" ticker.</p></li>
  <li><p>On the second problem, Joshua Ulrich points out in the comments that "Dates don't have timezones because days don't have a time component."</p></li>
  <li><p>On the third problem: The data frame appears to have corrupted dates, as pointed out by agstudy in the comments. </p></li>
  </ol>
  
  <p>My solutions rely on the quantmod package and the attached zoo/xts packages:</p>
  
  <pre><code>library(quantmod)
  </code></pre>
  
  <p>Here is the code I have used to get proper dates from my csv file:</p>
  
  <pre><code>toDate &lt;- function(x){ as.Date(as.character(x), format("%Y%m%d")) }
  dtz &lt;- read.zoo("myData.csv"
    , header = TRUE
    , sep = ","
    , FUN = toDate
  )
  dtx &lt;- as.xts(dtz)
  </code></pre>
  
  <p>The dates in the csv file were stored in a single column in the format "19861231". The key to getting correct dates was to wrap the date in "as.character()". Part of this code was inspired from <a href="https://stackoverflow.com/questions/9335917/r-stock-market-data-from-csv-to-xts">R - Stock market data from csv to xts</a>. I also found the zoo/xts manuals helpful.</p>
  
  <p>I then extract the date range from this dataset:</p>
  
  <pre><code>date.start &lt;- start(dtx)
  date.end &lt;- end(dtx)
  </code></pre>
  
  <p>I will use those dates with quantmod's getSymbols function so that the other data I download will cover the same period.</p>
  
  <p>Here is the code I have used to get all three tickers.</p>
  
  <pre><code>tickers &lt;- c("^GSPC","^TYX","DJIA")
  data &lt;- new.env() # the data environment will store the data
  do.call(cbind, lapply( tickers
      , getSymbols
      , from = date.start
      , to = date.end
      , env = data # data saved inside an environment
      )
    )
  ls(data)  # see what's inside the data environment
  data$GSPC  # access a particular ticker
  </code></pre>
  
  <p>Also note, as GSee pointed out in the comments, that the option <em>auto.assign=FALSE</em> cannot be used in conjunction with the option <em>env=data</em> (otherwise the download fails).</p>
  
  <p>A big thank you for your help.</p>