-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operational Metrics.R
36 lines (20 loc) · 1.12 KB
/
Operational Metrics.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
library("rvest") # Library
operating.ratios <- function(x){ # Function to get data for operational metrics
ors <- NULL # List for operational metrics values
for (q in 1:length(x)){ a <- x[q] # Each ticker in vector
is <- sprintf("https://finance.yahoo.com/quote/%s/financials?p=%s", a, a)
page.is <- read_html(is) # Read HTML & extract necessary info
price.yahoo1 <- page.is %>% html_nodes('div') %>% .[[1]] -> tab.is
y <- tab.is %>% html_nodes('div') %>% html_nodes('span') %>% html_text()
p <- c("Operating Income", "Tax Provision", "Interest Expense",
"Reconciled Depreciation", "EBITDA", "EBIT")
c <- NULL
for (m in 1:length(p)){ c <- rbind(c, y[grep(p[m], y) + 1][1]) }
c <- gsub(",", "", gsub("([a-zA-Z]),", "\\1 ", c))
w <- cbind(sum(as.numeric(c[seq(4)])), as.numeric(c[5]), as.numeric(c[6]))
colnames(w) <- c("OIBDA", "EBITDA", "EBIT") # Column names
ors <- rbind.data.frame(ors, w) } # Join to data frame
rownames(ors) <- x # Ticker names
ors # Display
}
operating.ratios(c("AAPL", "AIG", "C")) # Test