多个列表 JSON 到 R 中的数据框
Multiple list JSON to Data Frame in R
我正在从一个 API 中获取数据,其中 returns 一个 JSON 列表,其中包含不同长度的子列表。我想将结构展平为 R 中的数据框。下面是我迄今为止使用的代码,但是,我的每个 'rows' 或列表都包含不同数量的对象(列)。在 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)
使用 data.table::rbindList()
和 fill = TRUE
您还需要转置 t()
您的矩阵并在函数中转换为 data.frame
。
library(httr)
library(jsonlite)
rf <- GET("https://data.fortworthtexas.gov/resource/2ys6-nns2.json?fatality=True")
rfc <- content(rf)
json_file <- sapply(rfc, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
as.data.frame(t(x))
})
library(data.table)
data.table::rbindlist(json_file, fill= TRUE)
atintersection crashdatetime fatality hitandrun intersectingstreetblocknumber intersectingstreetname intersectingstreetsuffix location_1 reportnumber streetname
1: True 2018-09-30T04:30:00.000 True False 700 W.DICKSON ST <list> 18-87957 HEMPHILL
2: False 2018-10-18T19:49:00.000 True False 0 RIVERSIDE DR <list> 180093550
3: False 2018-10-18T00:22:00.000 True False 100 SILVER RIDGE BLVD E <list> 180093211 WHITE SETTLEMENT
4: False 2018-10-11T02:55:00.000 True False 5800 LOOP <list> 18-91258
5: False 2018-10-13T13:15:00.000 True False 1000 LUELLA ST <list> 18-91935 SOUTH
streetsuffix intersectingstreetprefix streetprefix intersectingstreetdescription streetdescription
1: ST
2: N NE
3: RD CONCRETE BLACKTOP
4: FWY E E
5: FWY ASPHALT ASPHALT
我正在从一个 API 中获取数据,其中 returns 一个 JSON 列表,其中包含不同长度的子列表。我想将结构展平为 R 中的数据框。下面是我迄今为止使用的代码,但是,我的每个 'rows' 或列表都包含不同数量的对象(列)。在 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)
使用 data.table::rbindList()
和 fill = TRUE
您还需要转置 t()
您的矩阵并在函数中转换为 data.frame
。
library(httr)
library(jsonlite)
rf <- GET("https://data.fortworthtexas.gov/resource/2ys6-nns2.json?fatality=True")
rfc <- content(rf)
json_file <- sapply(rfc, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
as.data.frame(t(x))
})
library(data.table)
data.table::rbindlist(json_file, fill= TRUE)
atintersection crashdatetime fatality hitandrun intersectingstreetblocknumber intersectingstreetname intersectingstreetsuffix location_1 reportnumber streetname
1: True 2018-09-30T04:30:00.000 True False 700 W.DICKSON ST <list> 18-87957 HEMPHILL
2: False 2018-10-18T19:49:00.000 True False 0 RIVERSIDE DR <list> 180093550
3: False 2018-10-18T00:22:00.000 True False 100 SILVER RIDGE BLVD E <list> 180093211 WHITE SETTLEMENT
4: False 2018-10-11T02:55:00.000 True False 5800 LOOP <list> 18-91258
5: False 2018-10-13T13:15:00.000 True False 1000 LUELLA ST <list> 18-91935 SOUTH
streetsuffix intersectingstreetprefix streetprefix intersectingstreetdescription streetdescription
1: ST
2: N NE
3: RD CONCRETE BLACKTOP
4: FWY E E
5: FWY ASPHALT ASPHALT