I am currently running the following code in order to extract from the quantmod
package some financial information.
library(quantmod)
symbols <- c("HOG", "GOOG", "GE")
tickers <- new.env()
lapply(symbols, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
df <- rbind(BS, IS, CF)
df <- t(df)
Which is a little messy but from here I can clean the data and proceed with some calculations. However I want to know if there is a more efficient way using the tidyquant
package as I would like to run this over many ticker symbols and it is currently breaking when the quantmod
package cannot download/find the financial information for a particular ticker.
I am working with;
library(tidyquant)
library(dplyr)
symbols <- c("HOG", "GOOG", "GE")
stock_financials <- symbols %>%
tq_get(get = "financials")
stock_financials$annual
I can see that the data is a tibble within a tibble but how is it possible to extract the information as before, or how can I more easily access the tibble data for stock_financials$annual
?
Modifying and using
filter(stock_financials, type == "BS") %>% unnest()
From this answer does not seem to work for me.
Here's a simple solution with gather
and unnest
from the tidyr
package. Once you do the gather()
and unnest()
combo you can then filter to whatever section and the combination of symbols you want.
> library(tidyquant)
> library(dplyr)
>
> symbols <- c("HOG", "GOOG", "GE")
>
> stock_financials <- symbols %>%
+ tq_get(get = "financials")
>
> stock_financials
# A tibble: 9 x 4
symbol type annual quarter
<chr> <chr> <list> <list>
1 HOG BS <tibble [168 x 4]> <tibble [210 x 4]>
2 HOG CF <tibble [76 x 4]> <tibble [76 x 4]>
3 HOG IS <tibble [196 x 4]> <tibble [245 x 4]>
4 GOOG BS <tibble [168 x 4]> <tibble [210 x 4]>
5 GOOG CF <tibble [76 x 4]> <tibble [76 x 4]>
6 GOOG IS <tibble [196 x 4]> <tibble [245 x 4]>
7 GE BS <tibble [168 x 4]> <tibble [210 x 4]>
8 GE CF <tibble [76 x 4]> <tibble [76 x 4]>
9 GE IS <tibble [196 x 4]> <tibble [245 x 4]>
>
> stock_financials %>%
+ gather(key = "key", value = "value", annual, quarter) %>%
+ unnest()
# A tibble: 2,913 x 7
symbol type key group category date value
<chr> <chr> <chr> <int> <chr> <date> <dbl>
1 HOG BS annual 1 Cash & Equivalents 2017-12-31 688.
2 HOG BS annual 1 Cash & Equivalents 2016-12-31 760.
3 HOG BS annual 1 Cash & Equivalents 2015-12-31 722.
4 HOG BS annual 1 Cash & Equivalents 2014-12-31 907.
5 HOG BS annual 2 Short Term Investments 2017-12-31 0.
6 HOG BS annual 2 Short Term Investments 2016-12-31 5.52
7 HOG BS annual 2 Short Term Investments 2015-12-31 45.2
8 HOG BS annual 2 Short Term Investments 2014-12-31 57.3
9 HOG BS annual 3 Cash and Short Term Investments 2017-12-31 688.
10 HOG BS annual 3 Cash and Short Term Investments 2016-12-31 766.
# ... with 2,903 more rows