I need some help with the quantmod
package for R and the interaction with the for loop.
I have the vector year
:
library(quantmod)
getFinancials("GE")
year <- colnames(viewFin(GE.f, "IS", "A"))
year
# [1] "2015-12-31" "2014-12-31" "2013-12-31" "2012-12-31"
The viewFin
function gives me this output:
viewFin(GE.f, type="IS", period="A")["Net Income", year[1]]
# Annual Income Statement for GE
# [1] -6126
But if I try to loop by indexing the year I get this error:
> for(x in 1:4){
+ (viewFin(GE.f, type="IS", period="A")["Net Income", year[x]])
+ x=x+1
+ }
Annual Income Statement for GE
Annual Income Statement for GE
Annual Income Statement for GE
Annual Income Statement for GE
Error in viewFin(GE.f, type = "IS", period = "A")["Net Income", year[x]] :
subscript out of bounds
My idea is to make the date, the financial names (net income, equity, etc.), and the symbol) to create a data frame with companies in rows and year & financial as columns.
Does the viewFin
function block the entry to the value of x
?
Note that you cannot alter an iterator inside a for loop in R. So your x = x + 1
at the end of the for loop is unnecessary, and ignored. Also note that auto-printing is disabled in for loops, so you need to explicitly call print
. Another thing you can do is iterate over a vector directly (there's no need to subset). So your for loop could look like this:
for(y in year) {
print(viewFin(GE.f, type="IS", period="A")["Net Income", y])
}
That said, the for loop is unnecessary. You could get the same result using subsetting directly.
netIncome <- viewFin(GE.f, type="IS", period="A")["Net Income",]
It may be problematic to make a data.frame with symbols as rows, and line item and date as columns, because there's no reason each symbol should have the same number of years of data, or the exact same line items. It would probably be better to put all the data in long format first, until you know what you're working with. Here's a function that does that for multiple symbols.
stackFinancials <-
function(symbols, type = c("BS", "IS", "CF"), period = c("A", "Q")) {
type <- match.arg(toupper(type[1]), c("BS", "IS", "CF"))
period <- match.arg(toupper(period[1]), c("A", "Q"))
getOne <- function(symbol, type, period) {
gf <- getFinancials(symbol, auto.assign = FALSE)
vf <- viewFinancials(gf, type = type, period = period)
df <- data.frame(vf, line.item = rownames(vf), type = type, period = period,
symbol = symbol, stringsAsFactors = FALSE, check.names = FALSE)
long <- reshape(df, direction="long", varying=seq(ncol(vf)), v.names="value",
idvar="line.item", times=colnames(vf))
rownames(long) <- NULL
long
}
# combine all into one data.frame
do.call(rbind, lapply(symbols, getOne, type = type, period = period))
}
And here's an example of using it:
R> Data <- stackFinancials(c("GE", "AAPL"), type = "IS", period = "A")
Annual Income Statement for GE
Annual Income Statement for AAPL
R> head(Data)
line.item type period symbol time value
1 Revenue IS A GE 2016-12-31 123693
2 Other Revenue, Total IS A GE 2016-12-31 NA
3 Total Revenue IS A GE 2016-12-31 123693
4 Cost of Revenue, Total IS A GE 2016-12-31 92508
5 Gross Profit IS A GE 2016-12-31 31185
6 Selling/General/Admin. Expenses, Total IS A GE 2016-12-31 18377
R> tail(Data)
line.item type period symbol time value
387 Effect of Special Items on Income Taxes IS A AAPL 2013-09-28 NA
388 Income Taxes Ex. Impact of Special Items IS A AAPL 2013-09-28 NA
389 Normalized Income After Taxes IS A AAPL 2013-09-28 NA
390 Normalized Income Avail to Common IS A AAPL 2013-09-28 NA
391 Basic Normalized EPS IS A AAPL 2013-09-28 NA
392 Diluted Normalized EPS IS A AAPL 2013-09-28 5.68