0

我正在按照FCC 的文档下载有关诉讼程序的一些元数据。

我不相信我可以发布数据,但您可以获得免费的API 密钥

我的代码生成 2 个列表的列表,而不是 JSON 格式的结构化 df。

我的目标是拥有一个数据框,其中每个 json 元素都是它自己的列.. 就像普通的 df 一样。

library(httr)
library(jsonlite)

datahere = "C:/fcc/"
setwd(datahere)

URL <- "https://publicapi.fcc.gov/ecfs/filings?api_key=<KEY HERE>&proceedings.name=14-28&sort=date_disseminated,DESC"
dataDF <- GET(URL)
dataJSON <- content(dataDF, as="text")
dataJSON <- fromJSON(dataJSON)

# NAs
dataJSON2 <- lapply(dataJSON, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

x <- do.call("rbind", dataJSON2)
x <- as.data.frame(x)
4

1 回答 1

2

JSON 确实嵌套很深,因此您需要多考虑一下在 list 和 data.frame 之间进行转换。下面的逻辑提取了一个包含 25 个文件(102 个变量)和 10 个聚合(25 个变量)的 data.frame。

# tackle the filings object
filings_df <- ldply(dataJSON$filings, function(x) {
  # removes null list elements
  x[sapply(x, is.null)] <- NA
  # converts to a named character vector
  unlisted_x <- unlist(x)
  # converts the named character vector to data.frame
  # with 1 column and rows for each element
  d <- as.data.frame(unlisted_x)
  # we need to transpose this data.frame because 
  # the rows should be columns, and don't check names when converting
  d <- as.data.frame(t(d), check.names=F)
  # now assign the actual names based on that original 
  # unlisted character vector
  colnames(d) <- names(unlisted_x)
  # now return to ldply function, which will automatically stack them together
  return(d)
})

# tackle the aggregations object
# same exact logic to create the data.frame
aggregations_df <- ldply(dataJSON$aggregations, function(x) {
  # removes null list elements
  x[sapply(x, is.null)] <- NA
  # converts to a named character vector
  unlisted_x <- unlist(x)
  # converts the named character vector to data.frame
  # with 1 column and rows for each element
  d <- as.data.frame(unlisted_x)
  # we need to transpose this data.frame because 
  # the rows should be columns, and don't check names when converting
  d <- as.data.frame(t(d), check.names=F)
  # now assign the actual names based on that original 
  # unlisted character vector
  colnames(d) <- names(unlisted_x)
  # now return to ldply function, which will automatically stack them together
  return(d)
})
于 2016-07-21T22:32:19.250 回答