diengiau
8/13/2017 - 5:21 AM

get_state_data.R

#' This version: 2017.08.09
#' 
#' This code help to download all the series by State in US from Fed Louis (FRED).
#' This code expands the quantmod's getSymbols function so we need it installed.
#' In addition, we also need reshape2 to manipulate data.
#' The only argument we need is the series symbol from FRED.
#' For example, UR for Unemployment rate, POP for population.
get_state_data <- function(symbol = "UR") {
  #load package:
  library(quantmod)
  library(reshape2)
  
  state = c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")
  
  #get symbols and download data
  state = paste0(state, symbol)
  
  df <- vector("list", length(state))
  for (i in seq_along(state)) {
    df[[i]] <- getSymbols(state[i], src="FRED")
  }
  
  #now manipulate data
  x <- ls()[str_detect(ls(), symbol)]
  
  if (length(x) != 50) {
    stop("Make sure we have 50 states in environment now", call. = FALSE)
  }
  
  #merge all 50 states
  df <- get(x[1])
  for (i in 2:50) {
    df <- merge(df, get(x[i]))
  }
  
  #manipulate to long-data
  df1 <- as.data.frame(df)
  df1$date = row.names(df1)
  df1 <- select(df1, date, everything())
  row.names(df1) <- NULL
  
  df2 <- melt(df1, id=c("date"))
  df2$variable <- str_sub(df2$variable, 1, 2)
  names(df2) <- c("date", "State", symbol)
  
  return(df2)
}