How do I extract the list of all stocks that were part of the S&P 500 index during the last N years (e.g., 20 years) in R? (or perhaps just get this list).
I know how to get the list of the current stocks:
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
tickers <- url %>%
read_html() %>%
html_nodes(xpath = '//*[@id="constituents"]') %>%
html_table()
sp500tickers <- tickers[[1]]
sp500tickers = sp500tickers %>% mutate(Symbol = case_when(Symbol == "BRK.B" ~ "BRK-B",
Symbol == "BF.B" ~ "BF-B",
TRUE ~ as.character(Symbol)))
symbols = sp500tickers$Symbol
print(symbols)
But how to get all the symbols during the last N years?