![](/img/trans.png)
[英]Creating for loop using tq_get function from the tidyquant library to get unemployment filings by state in R? [Image & Code Included]
[英]Getting 2020 unemployment numbers using blsAPI in R? [code and image included]
我正在嘗試從 R 中的 blsAPI 中提取 2020 年失業人數。我目前正在從這個鏈接復制這個例子。 我將結束年份更改為 2020 年,但我仍然只獲得截至 2016 年的失業數字作為最近一年。
這是代碼
#Install needed libraries
install.packages('rjson')
library(rjson)
install.packages('blsAPI')
library(blsAPI)
library(ggplot2)
## Pull the data via the API
payload <- list(
'seriesid'=c('LAUCN360610000000004', 'LAUCN360610000000006'),
'startyear'=2007,
'endyear'=2020)
response <- blsAPI(payload, 2)
json <- fromJSON(response)
## Process results
apiDF <- function(data){
df <- data.frame(year=character(),
period=character(),
periodName=character(),
value=character(),
stringsAsFactors=FALSE)
i <- 0
for(d in data){
i <- i + 1
df[i,] <- unlist(d)
}
return(df)
}
View(apiDF)
unemployed.df <- apiDF(json$Results$series[[1]]$data)
labor.force.df <- apiDF(json$Results$series[[2]]$data)
## Change value type from character to numeric
unemployed.df[,4] <- as.numeric(unemployed.df[,4])
labor.force.df[,4] <- as.numeric(labor.force.df[,4])
## Rename value prior to merging
names(unemployed.df)[4] <- 'unemployed'
names(labor.force.df)[4] <- 'labor.force'
## Merge data frames
df <- merge(unemployed.df, labor.force.df)
## Create date and unemployement rate
df$unemployment.rate <- df$unemployed / df$labor.force
df$date <- as.POSIXct(strptime(paste0('1',df$periodName,df$year), '%d%B%Y'))
## Beginning and end dates for the Great Recession (used in shaded area)
gr.start <- as.POSIXct(strptime('1December2007', '%d%B%Y'))
gr.end <- as.POSIXct(strptime('1June2009', '%d%B%Y'))
View(df)
##Plot out the data
ggplot(df) + geom_rect(aes(xmin = gr.start, xmax = gr.end, ymin = -Inf, ymax = Inf), alpha = 0.4, fill="#DDDDDD") + geom_line(aes(date, unemployment.rate*100)) + ylab('Percent of labor force') + xlab('Great Recession shaded in gray') + ggtitle('Unemployment Rate for Manhattan, NY (Jan 2007 to Dec 2010)') + theme_bw()
MichiganData <- blsQCEW('Area', year='2017', quarter='1', area='26000')
View(MichiganData)
package 似乎有兩個版本(版本 1 和版本 2),您使用的是哪一個?
對於版本 1,每個查詢的年數限制為 10 年。
鑒於您的開始日期是 2007 年,而您獲得的最新年份是 2016 年,您可能使用的是版本 1。
請參閱上面提供的鏈接中的 API Basics
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.