Skip to content

Instantly share code, notes, and snippets.

@kvasilopoulos
Last active August 12, 2019 16:48
Show Gist options
  • Save kvasilopoulos/7b0e297009def08d39a654a1a74ba8d0 to your computer and use it in GitHub Desktop.
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.
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