【发布时间】:2019-04-01 22:23:32
【问题描述】:
我从一个 API 获取数据,该 API 返回一个 JSON 列表,其中包含不同长度的子列表。我想将结构展平为 R 中的数据框。下面是我迄今为止使用的代码,但是,我的每个“行”或列表都包含不同数量的对象(列)。在 R 中执行此操作的最佳方法是什么?我假设它可能是对 sapply 函数的一些修改??
library(httr)
library(jsonlite)
rf <- GET("https://data.fortworthtexas.gov/resource/2ys6-nns2.json?fatality=True")
rfc <- content(rf)
rff <- fromJSON(rfc, simplifyDataFrame = TRUE)
json_file <- sapply(rfc, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
json_file$id <- rownames(json_file)
【问题讨论】:
标签: r json list dataframe sapply