需要帮助,从网站API使用RSocrata提取JSON数据 [英] Need help pulling JSON data with RSocrata from a website API
本文介绍了需要帮助,从网站API使用RSocrata提取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助起草代码,以直接从Socrata格式的网站中提取公共数据.这是链接:
I need help drafting code that pulls public data directly from a website that is in Socrata format. Here is a link:
有一个API端点:
https://data.cityofchicago.org/resource/xzkq-xp2w.json
上传数据后,年薪"中的空值应替换为50000.
After the data is uploaded, null values in the "Annual Salary" should be replaced with 50000.
推荐答案
我们可以使用RSocrata
包
library(RSocrata)
url <- "https://data.cityofchicago.org/resource/xzkq-xp2w.json"
data <- RSocrata::read.socrata(url)
head(data)
# name job_titles department full_or_part_time salary_or_hourly annual_salary typical_hours hourly_rate
#1 AARON, JEFFERY M SERGEANT POLICE F Salary 111444 <NA> <NA>
#2 AARON, KARINA POLICE OFFICER (ASSIGNED AS DETECTIVE) POLICE F Salary 94122 <NA> <NA>
#3 AARON, KIMBERLEI R CHIEF CONTRACT EXPEDITER DAIS F Salary 118608 <NA> <NA>
#4 ABAD JR, VICENTE M CIVIL ENGINEER IV WATER MGMNT F Salary 117072 <NA> <NA>
#5 ABARCA, FRANCES J POLICE OFFICER POLICE F Salary 48078 <NA> <NA>
以下内容将用50000
替换annual_salary
中的NA.
The following will replace the NAs in annual_salary
with 50000
.
data[is.na(data$annual_salary),"annual_salary"] <- 50000
但是,如果您想按照芝加哥市网站上的建议进行操作,则可以考虑将typical_hours
与hourly_rate
相乘以估算薪水.
However, if you'd like to do what it suggests on the city of Chicago website, you could consider multipling typical_hours
with hourly_rate
to estimate salary.
ind <- is.na(data$annual_salary)
data[ind,]$annual_salary <- as.numeric(data[ind,]$typical_hours) * as.numeric(data[ind,]$hourly_rate) * 52
这篇关于需要帮助,从网站API使用RSocrata提取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文