简体   繁体   English

使用 tidyquant 库中的 tq_get function 创建 for 循环,以获取 state 在 R 中的失业申请? [包括图像和代码]

[英]Creating for loop using tq_get function from the tidyquant library to get unemployment filings by state in R? [Image & Code Included]

Goal: I want to get initial unemployment filings by state in a table for every state in the US目标:我想在美国每个 state 的表格中获得 state 的初始失业申请

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'.我能够创建一个 csv 文件以将所有 state 缩写与“ICLAIMS”连接起来。 I simply want to pass a for loop through my function you see below.我只是想通过我的 function 传递一个 for 循环,如下所示。 Attached is a screenshot of the csv I uploaded that has all the states with in that format...'CAICLAIMS', 'NYICLAIMS', 'ALICLAIMS' and so on...附件是我上传的 csv 的屏幕截图,其中包含该格式的所有状态......'CAICLAIMS','NYICLAIMS','ALICLAIMS'等等......

在此处输入图像描述

We can create a function:我们可以创建一个 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 Claim_Code

lapply(df$Claim_Code, get_data)

If you want to combine this into one dataframe, we can do:如果您想将其合并为一个 dataframe,我们可以这样做:

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 .或使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Tidyquant 中使用 tq_get() 更改列名称 - Change column name with tq_get() in Tidyquant Tidyquant tq_get 和 tq_transmute:警告消息“从数据中删除的缺失值” - Tidyquant tq_get and tq_transmute: Warning message "missing values removed from data" 如何使用 R 中 tidyquant 包中的 tq_transmute 获取 OHLC 每月高低数据 - How to get OHLC monthly high and low data using tq_transmute from tidyquant package in R 有没有办法使用股票 ISIN 或使用 tq_get 的 SEDOL 提取 R 中的价格数据? (尤其是国际股票) - Is there a way to pull price data in R using an equities ISIN or SEDOL using tq_get? (international equities in particular) 在 r 中使用 tq_get 下载股票价格时如何修复错误? - How to fix the error when download stock price using tq_get in r? 在 R 中使用 blsAPI 获取 2020 年失业人数? [包括代码和图像] - Getting 2020 unemployment numbers using blsAPI in R? [code and image included] tq_get 在多次调用中返回不同日期的数据 - tq_get returning data with different dates on multiple calls getFinancials (quantmod) 和 tq_get (tidy quant) 不工作? - getFinancials (quantmod) and tq_get (tidy quant) not working? 警告:getSymbols、tq_get、getSymbols.yahoo 不返回迄今为止的价格 - Warning: getSymbols, tq_get, getSymbols.yahoo does not return prices for to date 如何在函数中传递要在tq_mutate(TidyQuant)中选择的变量列表? - How do you pass a list of variables to select in tq_mutate (TidyQuant) from within a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM