available from (https://rpubs.com/staszkiewicz/FM05_Financials)
How to get the financial statements into R
Let us set up the needed libraries. If you do not have installed any of such the libraries below you need first. install.packages(“edgar”, dependencies = FALSE), and replace “edgar”, which the name of the library is missing.
library(quantmod) # for basic quoting, the financial function is depreciated
## Ładowanie wymaganego pakietu: xts
## Ładowanie wymaganego pakietu: zoo
##
## Dołączanie pakietu: 'zoo'
## Następujące obiekty zostały zakryte z 'package:base':
##
## as.Date, as.Date.numeric
## Ładowanie wymaganego pakietu: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(finreportr) # for financials
library(edgar) # for full text retrial and text mining
library(XBRL) # for instants (full set manipulation)
library(dplyr) # for data manipulation
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Dołączanie pakietu: 'dplyr'
## Następujące obiekty zostały zakryte z 'package:xts':
##
## first, last
## Następujące obiekty zostały zakryte z 'package:stats':
##
## filter, lag
## Następujące obiekty zostały zakryte z 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)# for data manipulation and cleaning
library(finreportr)
library(XBRL)
# set up the authority to Edgar
options(HTTPUserAgent = "miky@xx.com") #EDGAR requires your e-mail to be shown"
# Coca cola KIK is 0000021344
# Get financial
AnnualReports(0000021344, foreign = FALSE)
## filing.name filing.date accession.no
## 1 10-K 2024-02-20 0000021344-24-000009
## 2 10-K 2023-02-21 0000021344-23-000011
## 3 10-K 2022-02-22 0000021344-22-000009
## 4 10-K 2021-02-25 0000021344-21-000008
## 5 10-K 2020-02-24 0000021344-20-000006
## 6 10-K 2019-02-21 0000021344-19-000014
## 7 10-K 2018-02-23 0000021344-18-000008
## 8 10-K 2017-02-24 0000021344-17-000009
## 9 10-K 2016-02-25 0000021344-16-000050
## 10 10-K 2015-02-25 0000021344-15-000005
## 11 10-K 2014-02-27 0000021344-14-000008
## 12 10-K 2013-02-27 0000021344-13-000007
## 13 10-K 2012-02-23 0000021344-12-000007
## 14 10-K 2011-02-28 0001047469-11-001506
## 15 10-K 2010-02-26 0001047469-10-001476
## 16 10-K 2009-02-26 0001047469-09-001875
## 17 10-K 2008-02-28 0001193125-08-041768
## 18 10-K 2007-02-21 0001047469-07-001328
## 19 10-K 2006-02-28 0001047469-06-002588
## 20 10-K 2005-03-04 0001047469-05-005411
## 21 10-K 2004-02-27 0001047469-04-005840
## 22 10-K 2003-03-26 0000021344-03-000031
## 23 10-K/A 2002-03-13 0000021344-02-000013
## 24 10-K 2002-03-11 0000021344-02-000011
## 25 10-K 2001-03-07 0000021344-01-000005
## 26 10-K 2000-03-09 0000021344-00-000009
## 27 10-K 1999-03-29 0000021344-99-000005
## 28 10-K 1998-03-09 0000021344-98-000004
## 29 10-K405 1997-03-11 0000021344-97-000014
## 30 10-K405 1996-03-14 0000021344-96-000006
## 31 10-K405 1995-03-13 0000021344-95-000007
## 32 10-K 1994-03-14 0000950144-94-000600
# now we get information about Coca Cola using CIK CompanyInfo(0000021344)
Co<-GetBalanceSheet('KO', 2015)
Note: you might put the CIK number of the Ticker of the company an full list of ticker you will find here: “https://www.sec.gov/file/company-tickers” i jonson format
We have no download the Coca Cola full K-10 and transferred to the object Co the balance sheet. Let us see it in an excel like form
View(Co)
The object is a data.frame but the numbers (value) are in character form to be able to make arithmetic on it we should convert it to numerical value. We might use the function ‘as.numeric’ to convert specific data from text to numbers. Let us see it how does it work:
# let us see the classs
class(Co)
## [1] "data.frame"
# let us see the class of values
class(Co$Amount)
## [1] "character"
# let us check if arithmetics works
# Check if the commend like 'Co$Amount/2' works
# let us convert from text (character) into values
as.numeric(Co$Amount)
## [1] 12803000000 8442000000 10414000000 8958000000 6707000000 9052000000
## [7] 17121000000 18010000000 3147000000 3665000000 4873000000 4466000000
## [13] 3277000000 3100000000 2886000000 3066000000 0 679000000
## [19] 31304000000 32986000000 10393000000 9947000000 1119000000 3678000000
## [25] 4661000000 4407000000 14476000000 14967000000 14633000000 6744000000
## [31] 6533000000 7415000000 6689000000 12255000000 12312000000 12100000000
## [37] 1140000000 1050000000 90055000000 92023000000 9577000000 9234000000
## [43] 16901000000 19130000000 1024000000 3552000000 309000000 400000000
## [49] 0 58000000 27811000000 32374000000 19154000000 19063000000
## [55] 3498000000 4389000000 6152000000 5636000000 1760000000 1760000000
## [61] 12276000000 13154000000 61660000000 63408000000 -3432000000 -5777000000
## [67] 39091000000 42225000000 33173000000 30320000000 267000000 241000000
## [73] 33440000000 30561000000 90055000000 92023000000
# now let us make e.g. divion on all classes
as.numeric(Co$Amount)/2
## [1] 6401500000 4221000000 5207000000 4479000000 3353500000 4526000000
## [7] 8560500000 9005000000 1573500000 1832500000 2436500000 2233000000
## [13] 1638500000 1550000000 1443000000 1533000000 0 339500000
## [19] 15652000000 16493000000 5196500000 4973500000 559500000 1839000000
## [25] 2330500000 2203500000 7238000000 7483500000 7316500000 3372000000
## [31] 3266500000 3707500000 3344500000 6127500000 6156000000 6050000000
## [37] 570000000 525000000 45027500000 46011500000 4788500000 4617000000
## [43] 8450500000 9565000000 512000000 1776000000 154500000 200000000
## [49] 0 29000000 13905500000 16187000000 9577000000 9531500000
## [55] 1749000000 2194500000 3076000000 2818000000 880000000 880000000
## [61] 6138000000 6577000000 30830000000 31704000000 -1716000000 -2888500000
## [67] 19545500000 21112500000 16586500000 15160000000 133500000 120500000
## [73] 16720000000 15280500000 45027500000 46011500000
# for simplicity we might create a second object Co1 where we copy the Co data and replace Amount column with number of Amount to do it we need a simple code like that
Co1<-Co
Co1$Amount<-as.numeric(Co$Amount)
Co1$Amount/5
## [1] 2560600000 1688400000 2082800000 1791600000 1341400000 1810400000
## [7] 3424200000 3602000000 629400000 733000000 974600000 893200000
## [13] 655400000 620000000 577200000 613200000 0 135800000
## [19] 6260800000 6597200000 2078600000 1989400000 223800000 735600000
## [25] 932200000 881400000 2895200000 2993400000 2926600000 1348800000
## [31] 1306600000 1483000000 1337800000 2451000000 2462400000 2420000000
## [37] 228000000 210000000 18011000000 18404600000 1915400000 1846800000
## [43] 3380200000 3826000000 204800000 710400000 61800000 80000000
## [49] 0 11600000 5562200000 6474800000 3830800000 3812600000
## [55] 699600000 877800000 1230400000 1127200000 352000000 352000000
## [61] 2455200000 2630800000 12332000000 12681600000 -686400000 -1155400000
## [67] 7818200000 8445000000 6634600000 6064000000 53400000 48200000
## [73] 6688000000 6112200000 18011000000 18404600000
# Below gives informaiton about the Google
CompanyInfo("GOOG")
## company CIK SIC state state.inc FY.end
## 1 Alphabet Inc. 0001652044 7370 CA DE 1231
## street.address city.state
## 1 1600 AMPHITHEATRE PARKWAY MOUNTAIN VIEW CA 94043
# this list the avaiable Tesla annual reports
AnnualReports("TSLA")
## filing.name filing.date accession.no
## 1 10-K 2024-01-29 0001628280-24-002390
## 2 10-K 2023-01-31 0000950170-23-001409
## 3 10-K/A 2022-05-02 0001564590-22-016871
## 4 10-K 2022-02-07 0000950170-22-000796
## 5 10-K/A 2021-04-30 0001564590-21-022604
## 6 10-K 2021-02-08 0001564590-21-004599
## 7 10-K/A 2020-04-28 0001564590-20-018984
## 8 10-K 2020-02-13 0001564590-20-004475
## 9 10-K 2019-02-19 0001564590-19-003165
## 10 10-K 2018-02-23 0001564590-18-002956
## 11 10-K 2017-03-01 0001564590-17-003118
## 12 10-K 2016-02-24 0001564590-16-013195
## 13 10-K 2015-02-26 0001564590-15-001031
## 14 10-K 2014-02-26 0001193125-14-069681
## 15 10-K 2013-03-07 0001193125-13-096241
## 16 10-K/A 2012-03-28 0001193125-12-137560
## 17 10-K 2012-02-27 0001193125-12-081990
## 18 10-K 2011-03-03 0001193125-11-054847
Homework