需要帮助使用 RSocrata 从网站 API 中提取 JSON 数据

Need help pulling JSON data with RSocrata from a website API

我需要帮助起草直接从 Socrata 格式的网站提取 public 数据的代码。这是一个 link:

https://data.cityofchicago.org/Administration-Finance/Current-Employee-Names-Salaries-and-Position-Title/xzkq-xp2w

有一个 API 端点:

https://data.cityofchicago.org/resource/xzkq-xp2w.json

数据上传后,"Annual Salary"中的空值应替换为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>

以下将 annual_salary 中的 NA 替换为 50000

data[is.na(data$annual_salary),"annual_salary"] <- 50000

但是,如果您想按照芝加哥市网站上的建议进行操作,可以考虑将 typical_hours 乘以 hourly_rate 来估算薪水。

ind <- is.na(data$annual_salary)
data[ind,]$annual_salary <- as.numeric(data[ind,]$typical_hours) * as.numeric(data[ind,]$hourly_rate) * 52