Created
August 28, 2019 12:59
-
-
Save kmader/4c8be1e74558ef1c8df2385ca8cec653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(quantmod) | |
library(rvest) | |
table_id <- 3 | |
url <- "https://en.wikipedia.org/wiki/S%26P_100" | |
sp100 <- url %>% | |
read_html() %>% | |
html_nodes("table") %>% | |
.[[table_id]] %>% | |
html_table() | |
sp100_ticker <- sp100$Symbol | |
sp100_company <- sp100$Name | |
quantmod::getSymbols(sp100_ticker) | |
sp100_data_complete <- list() | |
for(current_ticker in sp100_ticker) { | |
c_ticker<-try(get(current_ticker), silent=T) | |
sp100_data_complete[[length(sp100_data_complete) + 1]] <- c_ticker | |
} | |
rows <- sapply(sp100_data_complete, function(x) nrow(x)) | |
nrow.cutoff <- 1500 | |
nrows <- unlist(rows) | |
pos <- which(nrows < nrow.cutoff) | |
sp100_ticker[pos] | |
sp100_data_complete <- sp100_data_complete[-pos] | |
sp100_company <- sp100_company[-pos] | |
sp100_ticker <- sp100_ticker[-pos] | |
current_pos <- 1 | |
sp100_adjusted <- Ad(sp100_data_complete[[current_pos]]) | |
for(current_pos in 2:length(sp100_ticker)) { | |
sp100_adjusted <- merge(sp100_adjusted, | |
Ad(sp100_data_complete[[current_pos]])) | |
} | |
names(sp100_adjusted) <- sp100_ticker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment