Search code examples
rquantmod

Getting specific line from Cash Flow


I'm trying to get data from Cash Flow with getFinancials function in R.

library(quantmod)
s <- c("AAPL","GOOG","IBM","GS","AMZN","GE")
fin <- lapply(s, getFinancials, auto.assign=FALSE)
names(fin) <- s

But when I try to get a specific line with

fin$AAPL$CF$A["Cash from Operating Activities"]
fin$AAPL$CF$A["Capital Expenditures"]

I get a NA return. How can I get these specific lines from the Cash Flow?


Solution

  • Since fin$AAPL$CF$A is a matrix, you need a comma after the name because you are asking for a row name. Without a comma you are asking for a vector element named "Cash from Operating Activities", and since we have a matrix the individual elements are not named.

    class(fin$AAPL$CF$A)
    # [1] "matrix"
    "Cash from Operating Activities" %in% rownames(fin$AAPL$CF$A)
    # [1] TRUE
    
    ## no comma - NA because "Cash from Operating Activities" is not a named vector element
    fin$AAPL$CF$A["Cash from Operating Activities"]
    # [1] NA
    

    This can be more easily illustrated with

    x <- 1
    x["x"]
    # [1] NA
    

    Since x has no named element "x", we get NA. You want the following.

    ## with comma - asking for the row "Cash from Operating Activities"
    fin$AAPL$CF$A["Cash from Operating Activities", ]
    # 2014-09-27 2013-09-28 2012-09-29 2011-09-24 
    #      59713      53666      50856      37529 
    

    Since these are matrices, rows are requested on the left side of the comma, columns on the right, and single named elements with no comma.