Goal: I want to get initial unemployment filings by state in a table for every state in the U.S.
Here is the example I am following: Link
Here is a quick snippet of the code from that link:
ga_claims <-
"GAICLAIMS" %>%
tq_get(get = "economic.data",
from = "1999-01-01") %>%
rename(claims = price)
That first filter is for Georgia. However, I want it for all states. I was able to create a csv file to concatenate all state abbreviations with the 'ICLAIMS'. I simply want to pass a for loop through my function you see below. Attached is a screenshot of the csv I uploaded that has all the states with in that format...'CAICLAIMS', 'NYICLAIMS', 'ALICLAIMS' and so on...
We can create a function :
library(dplyr)
library(tidyquant)
get_data <- function(x) {
x %>%
tq_get(get = "economic.data",from = "1999-01-01") %>%
rename(claims = price)
}
and pass each Claim_Code
through lapply
.
lapply(df$Claim_Code, get_data)
If you want to combine this into one dataframe, we can do :
do.call(rbind, Map(cbind, lapply(df$Claim_Code, get_data),
Claim_Code = df$Claim_Code))
# date claims Claim_Code
#1 1999-01-02 9674 GAICLAIMS
#2 1999-01-09 19455 GAICLAIMS
#3 1999-01-16 20506 GAICLAIMS
#4 1999-01-23 12932 GAICLAIMS
#5 1999-01-30 10871 GAICLAIMS
#6 1999-02-06 7997 GAICLAIMS
OR using purrr
.
library(purrr)
map2_df(map(df$Claim_Code, get_data), df$Claim_Code, cbind)
data
df <- data.frame(Claim_Code = c('GAICLAIMS', 'ALICLAIMS', 'AZICLAIMS'),
stringsAsFactors = FALSE)