I am having a bit of an issue turning a list into a tidy data frame.
I am scraping data from yahoo finance and turning it into a data frame.
library(tidyverse)
library(rvest)
library(httr)
library(quantmod)
ticker = "SPY"
spydf = read_html("https://ca.finance.yahoo.com/quote/SPY?p=SPY&.tsrc=fin-srch") %>% html_nodes("table") %>% .[2] %>% html_table(fill = T)
print(spydf)
[[1]]
X1 X2
1 Net Assets 268.03B
2 NAV 300.76
3 PE Ratio (TTM) N/A
4 Yield 1.85%
5 YTD Return 21.76%
6 Beta (3Y Monthly) 1.00
7 Expense Ratio (net) 0.09%
8 Inception Date 1993-01-22
class(spydf)
"list"
I would like the final product to be the transpose of the above. Where X1 are the headers and X2 are the columns.
#Something like this.
Net_Assets NAV Yield Beta Expense_Ratio ....
1 286.03 300.76 1.85 1 0.09 ...
Here is what I have tried and it does not seem to work
spy = read_html("https://ca.finance.yahoo.com/quote/SPY?p=SPY&.tsrc=fin-srch") %>% html_nodes("table") %>% .[2] %>% html_table(fill = T) %>% as.data.frame()
spy = t(spy) %>% as.data.frame()
colnames(spy) = spy[1,]
spy = spy[-1,]
print(spy)
c(X1 = 2) c(X1 = 2) c(X1 = 2) c(X1 = 2) c(X1 = 2) c(X1 = 2)
X2 268.03B 300.76 N/A 1.85% 21.76% 1.00
c(X1 = 2) c(X1 = 2)
X2 0.09% 1993-01-22
I have tried others but I will try and keep this clean.