【问题标题】:How can I work with this nested JSON format in R?如何在 R 中使用这种嵌套的 JSON 格式?
【发布时间】:2019-02-24 18:39:36
【问题描述】:

我正在尝试使用类似于下面的嵌套 JSON 格式。我最终需要的是数据框只有两行数据,一行给 John,一行给 Sam,其他数据格式如下。所以这个特定的数据框将有 2 行和 7 列。

Name  RD1  RD2  Hours1  Hours2  Billable1  Billable2
John
Sam

如何做到这一点?提前谢谢!

代码如下:

library(jsonlite)
options(stringsAsFactors = FALSE)
    rawData <- "document.txt"
    processedData <- fromJSON(rawData, flatten = TRUE)
    processedData <- processedData[, c("name", "records")]
    unnestedJSON <- unnest(processedData, records)

document.txt 包含以下信息:

[
{
  "name": "John",
  "records": [
     {
        "reportDate": "2018-07-20",
        "hours": 204,
        "billable": 32844
     },
     {
        "reportDate": "2018-03-25",
        "hours": 234,
        "billable": 37715
     }
  ]
},
{
  "name": "Sam",
  "records": [
     {
        "reportDate": "2018-06-18",
        "hours": 187,
        "billable": 13883
     },
     {
        "reportDate": "2018-04-02",
        "hours": 176,
        "billable": 13467
     }
  ]
}
]

【问题讨论】:

    标签: r json dataframe nested


    【解决方案1】:

    你的意思是这样的吗?

    jsonlite::fromJSON(s, flatten=TRUE) %>%
      tidyr::unnest()
    #   name reportDate hours billable
    # 1 John 2018-07-20   204    32844
    # 2 John 2018-03-25   234    37715
    # 3  Sam 2018-06-18   187    13883
    # 4  Sam 2018-06-19   188    13884
    # 5  Sam 2018-06-20   189    13885
    # 6  Sam 2018-04-02   176    13467
    

    我很难以一般方式直接使用data.table(事先不知道列),请参阅https://stackoverflow.com/a/34693087/3358272(在此处不起作用)和https://github.com/Rdatatable/data.table/issues/2146以供参考。


    支持数据,添加了一些条目:

    s <- '[
    {
      "name": "John",
      "records": [
         {
            "reportDate": "2018-07-20",
            "hours": 204,
            "billable": 32844
         },
         {
            "reportDate": "2018-03-25",
            "hours": 234,
            "billable": 37715
         }
      ]
    },
    {
      "name": "Sam",
      "records": [
         {
            "reportDate": "2018-06-18",
            "hours": 187,
            "billable": 13883
         },
         {
            "reportDate": "2018-06-19",
            "hours": 188,
            "billable": 13884
         },
         {
            "reportDate": "2018-06-20",
            "hours": 189,
            "billable": 13885
         },
         {
            "reportDate": "2018-04-02",
            "hours": 176,
            "billable": 13467
         }
      ]
    }
    ]'
    

    【讨论】:

      【解决方案2】:

      您只需要将数据聚合到名称级别(假设每个名称都是唯一的)。

      这是data.table的选项:

      library(data.table)
      dt <- data.table(unnestedJSON)
      dt[, .(RD1 = first(reportDate), 
             RD2 = last(reportDate),  
             Hours1 = first(hours)
             Hours2 = last(hours)
             Billable1 =first(billable) 
             Billable2 = last(billable)), by = name]
      

      如果你的名字不是唯一的,你需要先创建一个 ID 列并按 ID 聚合。

      注意:这应该适用于您的示例,每个名称有两个值。如果 json 每个名称可以有更多的值,它可能不符合您的要求。

      要支持每个名称超过两个值,请使用 dcast:

      library(data.table)
      dt <- data.table(unnestedJSON)
      dt[, rown := 1:.N, by = name]
      res <- dcast(
        dt, 
        formula = name ~ rown, 
        value.var = c("reportDate", "hours", "billable"), fun.aggregate = mean)
      

      【讨论】:

        猜你喜欢
        • 2020-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-23
        相关资源
        最近更新 更多