chollida
chollida

Reputation: 7894

R convert string to variable

I'm using the quantmod package in R to pull historical data from yahoo finance.

To use the package I create a list of tickers like so:

symbols <- c("MSFT", "ORCL", "AAPL", "FB")

To get the historical data I call the quantlib method getSymbols:

try(getSymbols(sym, src="yahoo"))

This creates variables in my environment called MSFT, ORCL, APPL and FB.

To calculate a correlation between MSFT and ORCL with closing prices I can use

cor(Cl(MSFT), Cl(ORCL))

To get:

           ORCL.Close
MSFT.Close  0.6597159

How can I make this generic so that i can pull say 20 symbols and run the correlation between them?

I don't know how to refer to a variable given a string. ie I have the string "MFST", how do I refer to the variable MSFT ?

I've got the string "MFST" and "ORCL" how can I use that to write cor(Cl(MSFT), Cl(ORCL))

Upvotes: 1

Views: 411

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145785

The get() function does what you're looking for, e.g. get("MSFT").

Generally, you might do better to store everything in a list rather than in the global environment. If you want to do something to all of them having them in a list makes it a bit easier. I talk a little more about that in this answer, though I'm sure there are better discussions out there.

Edit: In this case, rather than a list, putting things in their own environment as in @Josh Ulrich's answer is probably best.

Upvotes: 2

Joshua Ulrich
Joshua Ulrich

Reputation: 176668

The easiest way is to store the data in an environment, and then loop over all the objects in the environment with eapply:

library(quantmod)
dataEnv <- new.env()
getSymbols("MSFT;ORCL;AAPL;FB", env=dataEnv)
# Extract the close column from all objects,
# then merge all close columns into one object
cl <- do.call(merge, eapply(dataEnv, Cl))
# note the warning in the Details section of ?cor
# regarding the use of "pairwise.complete.obs"
cor(cl, use="pairwise.complete.obs")
#            AAPL.Close ORCL.Close MSFT.Close   FB.Close
# AAPL.Close  1.0000000  0.6467283  0.2528689 -0.5997337
# ORCL.Close  0.6467283  1.0000000  0.6597159  0.8211939
# MSFT.Close  0.2528689  0.6597159  1.0000000  0.8979836
# FB.Close   -0.5997337  0.8211939  0.8979836  1.0000000

Upvotes: 7

Related Questions