R quantmod::getFinancials

quantmod, r, xts

Solution

Anaother option is to laod your tickers in an new environnement.

tickers <-  new.env()
s <- c("AAPL","GOOG","IBM","GS","AMZN","GE")
lapply(s, getFinancials,env=tickers)
sapply(ls(envir=tickers),
       function(x) {x <- get(x) ## get the varible name
                    x$IS$A["Operating Income", ] / x$IS$A["Total Revenue",]})

              AAPL.f     AMZN.f       GE.f    GOOG.f      GS.f     IBM.f
2012-09-29 0.3529596 0.01106510 0.11811969 0.2543099 0.2689852 0.2095745
2011-09-24 0.3121507 0.01792957 0.13753327 0.3068724 0.1676678 0.1964439
2010-09-25 0.2818704 0.04110630 0.09415548 0.3540466 0.2804621 0.1974867
2009-09-26 0.2736278 0.04606471 0.06387029 0.3514585 0.3837401 0.1776439

EDIT

No need to use `ls`, `get`.... just the handy `eapply` (thanks @GSee) which applies FUN to the named values from an environment and returns the results as a list

eapply(tickers, function(x) 
              x$IS$A["Operating Income", ] / x$IS$A["Total Revenue",])

Problem

I'm using the `quantmod`package. I've got a vector of tickers like this : ``` c("AAPL","GOOG","IBM","GS","AMZN","GE") ``` and I want to create a function to calculate the EBIT margin of a stock (= operating income / total revenue). So for a given stock, I use the following piece of code which only works for GE (provided a ".f" is added a the end of the ticker) : ``` require(quantmod) getFinancials("GE",period="A") ebit.margin <- function(stock.ticker.f){ return(stock.ticker$IS$A["Operating Income",]/stock.ticker$IS$A["Total Revenue",]) } ebit.margin("GE") ``` I would like to generalize this function in order to use then the `apply`function. There are several difficulties : - when applying the `quantmod::getFinancial`function to a ticker, the financial statements of the stocks are saved in the default environment. The `viewFinancial`has then to be used to get and print the financial statements. I need a way to get access to the financial statements directly into the function - The function's argument function is a string like "GE.f" but it would more convenient to enter directly the ticker ("GE"). I've tried to use the `paste0` and `gsub` to get a string like "GE.f" it doesn't work because "GE.f" doesn't belong to the `financials` class. To sum up, I'm a bit lost...

Original source