1

I looked many entries on merging R data frames, however they are not clear to me, they talk about merging/joining using a common column, but in my case its missed or may I don't know how to extract. Here is what I am doing.

library(quantmod)
library(xts)
start = '2001-01-01'
end = '2015-08-14'
ticker = 'AAPL'
f = getSymbols(ticker, src = 'yahoo', from = start, to = end, auto.assign=F)
rsi14 <- RSI(f$AAPL.Adjusted,14)

The output I am expecting is all the columns of f and rsi14 match by date, however 'date' is not available as column, so not sure how do I join. I have to join few Moving Average columns as well.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 2
    People are volunteering here to help, please do not make us install packages and download data in order just to see what the data looks like. I suggest you read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [this](http://stackoverflow.com/help/mcve) and then provide some more information. – r2evans Aug 15 '15 at 04:44
  • 2
    The premise of your question is wrong, because `getSymbols` returns an xts object, not a data.frame. Merging xts objects is as simple as `f <- merge(f, rsi14)`. – Joshua Ulrich Aug 15 '15 at 04:47
  • @JoshuaUlrich I think it would be good if you posted this comment as an answer. I keep seeing posts where people try to convert xts objects (which are generated by `quantmod` in this format for a good reason) into data.frames. – RHertel Aug 15 '15 at 05:42
  • 2
    @RHertel: will do, but then I'm going to bed. :) – Joshua Ulrich Aug 15 '15 at 05:45

1 Answers1

3

The premise of your question is wrong. getSymbols returns an xts object, not a data.frame:

R> library(quantmod)
R> f <- getSymbols("AAPL", auto.assign=FALSE)
R> str(f)
An ‘xts’ object on 2007-01-03/2015-08-14 containing:
  Data: num [1:2170, 1:6] 86.3 84 85.8 86 86.5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2015-08-15 00:46:49"

xts objects do not have a "Date" column. They have an index attribute that holds the datetime. xts extends zoo, so please see the zoo vignettes as well as the xts vignette and FAQ for information about how to use the classes.

Merging xts objects is as simple as:

R> f <- merge(f, rsi14=RSI(Ad(f), 14))

Or you could just use $<- to add/merge a column to an existing xts object:

R> f$rsi14 <- RSI(Ad(f), 14)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • thanks, one more question, how do I retrieve the DATE column from the XTS object, I saw in another thread you had mentioned to use index(f) where f is an XTS object, index(f) is returning date as an array, but I am looking at adding it as another column along with other columns returned by XTS object - thanks again. – gvphubli.blogspot.com Aug 17 '15 at 00:21
  • @user4488573: There is no date column. xts/zoo objects are a matrix with an index attribute, which is where the date is stored. Don't add it as another column too. You can't mix types in a matrix, and a matrix can't contain a classed object anyway, so your Date would be converted to integer/numeric. – Joshua Ulrich Aug 17 '15 at 01:02