Search code examples
rquantmod

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))


Solution

  • 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