【问题标题】:loop within a loop for JSON files in R在 R 中的 JSON 文件的循环内循环
【发布时间】:2017-02-03 00:04:06
【问题描述】:

我正在尝试将一堆 JSON 文件聚合成一个用于三个来源和三年的文件。虽然到目前为止我只能通过乏味的方式做到这一点,但我相信我可以用更聪明、更优雅的方式来做到这一点。

json1 <- lapply(readLines("NYT_1989.json"), fromJSON)
json2 <- lapply(readLines("NYT_1990.json"), fromJSON)
json3 <- lapply(readLines("NYT_1991.json"), fromJSON)
json4 <- lapply(readLines("WP_1989.json"), fromJSON)
json5 <- lapply(readLines("WP_1990.json"), fromJSON)
json6 <- lapply(readLines("WP_1991.json"), fromJSON)
json7 <- lapply(readLines("USAT_1989.json"), fromJSON)
json8 <- lapply(readLines("USAT_1990.json"), fromJSON)
json9 <- lapply(readLines("USAT_1991.json"), fromJSON)

jsonl <- list(json1, json2, json3, json4, json5, json6, json7, json8, json9)

请注意,从 1989 年到 1991 年,三个文件的年份周期相同。有什么想法吗?谢谢!

PS:每个文件内的数据示例:

{"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "Frigid temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. ", "title": "Prospects;"}
{"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "DATELINE: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' ", "title": "Upheaval in the East: Espionage;"}
{"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "SURVIVING the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. ", "title": "Coping With the Economic Prospects of 1990"}

【问题讨论】:

  • 使用list.files 获取文件名列表,然后使用lapply(FILELIST, function(x) fromJSON(readLines(x)))?

标签: json r list loops


【解决方案1】:

给你:

require(jsonlite)

filelist <- c("NYT_1989.json","NYT_1990.json","NYT_1991.json",
              "WP_1989.json", "WP_1990.json","WP_1991.json",
              "USAT_1989.json","USAT_1990.json","USAT_1991.json")

newJSON <- sapply(filelist, function(x) fromJSON(readLines(x)))

仅从输入文件的每一行读取body 条目。

您询问了如何仅读取 JSON 文件的子集。引用的文件数据实际上不是 JSON 格式。它类似于 JSON,因此我们必须将输入修改为 fromJSON() 才能正确读取数据。我们从fromJSON()$body 中取消引用结果以仅提取body 变量。

filelist <- c("./data/NYT_1989.json", "./data/NYT_1990.json")
newJSON <- sapply(filelist, function(x) fromJSON(sprintf("[%s]", paste(readLines(x), collapse = ",")), flatten = FALSE)$body)
newJSON

结果

> filelist <- c("./data/NYT_1989.json", "./data/NYT_1990.json")
> newJSON <- sapply(filelist, function(x) fromJSON(sprintf("[%s]", paste(readLines(x), collapse = ",")), flatten = FALSE)$body)
> newJSON
     ./data/NYT_1989.json                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[1,] "Frigid temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. "
[2,] "DATELINE: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' "                                                                                                                                                
[3,] "SURVIVING the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. "                                                                                                                                                                                                                  
     ./data/NYT_1990.json                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[1,] "Blue temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. "
[2,] "BLUE1: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' "                                                                                                                                                 
[3,] "GREEN4 the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. "

您可能会发现以下应用教程很有用:

我也推荐阅读:

当我说这本在线免费书籍对我帮助很大时,请相信我。它还多次证实我是个白痴:-)

【讨论】:

  • 非常感谢@Technophobe01 的提示和答案!还有一点,每个文件中的每篇文章都是由“标题”和“正文”组成的。我怎么能只聚合身体?任何想法,干杯!
  • Andrea 你有数据的例子吗?您想加载和发布过滤器,还是将过滤器作为加载的一部分?退房:zevross.com/blog/2015/02/12/…。如果您有数据文件,我可以在今天下午更新答案。
  • 我想过滤作为负载的一部分。对每个文件进行聚合,但只是将每篇文章的正文添加到列表中。我已经用数据示例编辑了问题
猜你喜欢
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
相关资源
最近更新 更多