[R] use vector to merge multiple xts objects?

jctoll at gmail.com jctoll at gmail.com
Thu Dec 16 23:15:22 CET 2010


Hi,

I have several xts objects that are historical quotes downloaded as such:

library(quantmod)

> getSymbols("AA")

> head(AA)
           AA.Open AA.High AA.Low AA.Close AA.Volume AA.Adjusted
2007-01-03   30.05   30.06  29.17    29.33   8176300       26.89
2007-01-04   29.33   29.40  28.81    29.11   5655800       26.69
2007-01-05   29.11   29.24  28.49    28.76   7453100       26.37
2007-01-08   28.87   29.02  28.28    28.48  10276100       26.12
2007-01-09   28.70   28.88  28.09    28.52  12511700       26.15
2007-01-10   29.57   30.36  29.21    30.23  23283400       27.72

For the sake of simplicity, let's say I have 3 times series - AA, AXP,
and BA.  Further, let's say I have a vector that contains these three
symbols which is also their object name.

> x <- c("AA", "AXP", "BA")

What I would like to be able to do is it use that vector to "copy" one
column from each of my xts objects (AA, AXP, & BA) and to create a new
xts object.  It would look just like the output from this command, but
I want to do this without having to enumerate each object.  Rather, I
would like to be able to simply pass the vector of tickers "x", and
then specify the relevant column.

> head(merge(AA[,1], AXP[,1], BA[,1]))
           AA.Open AXP.Open BA.Open
2007-01-03   30.05    61.18   88.90
2007-01-04   29.33    60.23   88.34
2007-01-05   29.11    59.65   89.78
2007-01-08   28.87    59.03   88.61
2007-01-09   28.70    59.90   88.93
2007-01-10   29.57    59.24   88.04

Is there a way that this can be achieved without using a loop to merge
them together one by one?  Thank you.

James



More information about the R-help mailing list