Last active
August 12, 2019 16:48
-
-
Save kvasilopoulos/7b0e297009def08d39a654a1a74ba8d0 to your computer and use it in GitHub Desktop.
Download Historical S&P500 Price and Dividends from Shiller's dataset. The output is a formatted data.frame.
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
pkgs <- c("readxl", "lubridate", "zoo", "dplyr") | |
lapply(pkgs, require, character.only = TRUE) | |
temp <- paste0(tempfile(), ".xls") | |
file <- download.file("http://www.econ.yale.edu/~shiller/data/ie_data.xls", | |
destfile = temp, mode = "wb") | |
sp500 <- read_excel(path = temp, | |
sheet = "Data", | |
col_types = c("text", "skip", "skip", "skip", | |
"skip", "skip", "skip", "numeric", | |
"numeric", "skip", "skip"), skip = 7) %>% | |
na.omit() %>% | |
dplyr::mutate( Date = Date %>% | |
zoo::as.yearmon("%Y.%m") %>% | |
lubridate::myd(truncated = 1)) %>% | |
dplyr::mutate(Ratio = Price/Dividend) %>% | |
as.data.frame() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment