带有不规则节点的分层data.frame到JSON [英] Hierarchical data.frame to JSON with irregular nodes

查看:95
本文介绍了带有不规则节点的分层data.frame到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个嵌套的数据集:

I have this nested data set:

grandparent,parent,child,grandchild,age
Grandma,,,,100
Grandma,John,,,72
Grandma,John,Jessica,,41
Grandma,John,Joanne,,35
Grandma,Mary,,,70
Grandma,Mary,Max,,39
Grandma,Mary,Max,Ken,12
Grandma,Mary,Max,Kate,19
Grandma,Mary,Max,Karl,8
Grandma,Mary,Millie,Peter,2
Grandma,Mary,Millie,Pat,11
Grandma,Mary,Millie,Pam,24
Grandma,Dave,,,66
Grandma,Dave,Doloris,,32
Grandma,Dave,Dana,,23
Grandma,Dave,Daniel,,13

我想将其转换为分层JSON 结构,例如

which I would like to convert to a hierarchical JSON structure a la flare.json for D3.js, although keeping the value 'age' at each node, like below. I know this subject is not entirely novel - but I haven't seen any solutions where the nodes can be irregular and getting the value for each node..

{
 "name":"Grandma",
 "age": 100,
 "children":[
   {
     "name":"John",
      "age": 72,
     "children":[
        {
           "name":"Jessica",
           "age": 41
        },
        {
           "name":"Joanne",
           "age": 35
        }
    ]   
},
    {
     "name":"Mary",
     "age": 70,
     "children":[
        {
           "name":"Max",
           "age": 39
            "children":[
                {
                   "name":"Ken",
                   "age":12
                },
                {
                   "name":"Kate",
                   "age":19
                },
                {
                   "name":"Karl",
                   "age":8
                }
             ]
        },
        {
           "name":"Millie",
           "age":43,
           "children":[
                {
                   "name":"Peter",
                   "age":2
                },
                {
                   "name":"Pat",
                   "age":11
                },
                {
                   "name":"Pam",
                   "age":24
                }
             ]
        }
     ]
  },
  {
     "name":"Dave",
     "age": 66,
     "children":[
        {
           "name":"Doloris",
           "age":32
        },
        {
           "name":"Dana",
           "age":23
        },
        {
           "name":"Daniel",
           "age":13
        }
     ]
  }
  ]
 }

data.frame:

The data.frame:

structure(list(grandparent = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Grandma", class = "factor"), 
        parent = structure(c(1L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
        4L, 4L, 4L, 2L, 2L, 2L, 2L), .Label = c("", "Dave", "John", 
        "Mary"), class = "factor"), child = structure(c(1L, 1L, 5L, 
        6L, 1L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 1L, 4L, 2L, 3L), .Label = c("", 
        "Dana", "Daniel", "Doloris", "Jessica", "Joanne", "Max", 
        "Millie"), class = "factor"), grandchild = structure(c(1L, 
        1L, 1L, 1L, 1L, 1L, 4L, 3L, 2L, 7L, 6L, 5L, 1L, 1L, 1L, 1L
        ), .Label = c("", "Karl", "Kate", "Ken", "Pam", "Pat", "Peter"
        ), class = "factor"), age = c(100L, 72L, 41L, 35L, 70L, 39L, 
        12L, 19L, 8L, 2L, 11L, 24L, 66L, 32L, 23L, 13L)), .Names = c("grandparent", 
    "parent", "child", "grandchild", "age"), class = "data.frame", row.names = c(NA, 
    -16L))

推荐答案

在调用toJSON之前,需要将data.frame转换为参差不齐的列表.尝试以下

You need to convert the data.frame to a ragged list before calling toJSON. Try the following

library(toJSON)
library(data.table)

# Convert the data.frame to data.table for ease of handling
DT <- data.table(D)  # assuming `D` is your original data.frame

rList <- list()
parent <- list()
child <- list()
grandchild <- list()

j <- 1
while (j < nrow(DT)) {
  lastj <- j

  while (DT[j, parent=="" && grandparent != ""]) {
    rList[[length(rList)+1]] <- as.list(DT[j, list(name=grandparent, age)])
    j <- j+1
  }

  while (DT[j, child==""] && DT[(j-1):j, identical(grandparent[[1]], grandparent[[2]])]  && (j < nrow(DT)) ) {
    parent[[length(parent)+1]] <- as.list(DT[j, list(name=parent, age)])
    j <- j+1
  }

  while(DT[j, grandchild==""] && DT[(j-1):j, identical(parent[[1]], parent[[2]])]  && (j < nrow(DT)) ) {
    child[[length(child)+1]] <- as.list(DT[j, list(name=child, age)])
    j <- j+1
  }

  while(DT[j, grandchild!="" ] && DT[(j-1):j, identical(child[[1]], child[[2]])]  && (j < nrow(DT)) ) {
    grandchild[[length(grandchild)+1]] <- as.list(DT[j, list(name=grandchild, age)])
    j <- j+1
  }

  if (length(grandchild)) {
    child[[length(child)]][["children"]] <- grandchild
#    grandchild <- list() # reset
  }
  if (length(child)) {
    parent[[length(parent)]][["children"]] <- child
#    child <- list()
  }
  if (length(parent)) {
    rList[[length(rList)]][["children"]] <- parent
#    parent <- list()
  }

  cat ("\tat end, j  = ", j, "\n")

  # if j wasn't incremented throughout the whole loop, do so now. (This will happen when there is a change in the penultimate level)
  if (j == lastj)
    j <- j+1

}

RESULTS <- toJSON(rList)

结果:

cat(gsub("\\{","\n\\{", RESULTS))

[
{"name":"Grandma","age":100,"children":[
{"name":"John","age":72,"children":[
{"name":"Jessica","age":41},
{"name":"Joanne","age":35}]},
{"name":"Mary","age":70,"children":[
{"name":"Jessica","age":41},
{"name":"Joanne","age":35},
{"name":"Max","age":39,"children":[
{"name":"Ken","age":12},
{"name":"Kate","age":19},
{"name":"Karl","age":8},
{"name":"Pat","age":11},
{"name":"Pam","age":24}]}]},
{"name":"Dave","age":66,"children":[
{"name":"Jessica","age":41},
{"name":"Joanne","age":35},
{"name":"Max","age":39,"children":[
{"name":"Ken","age":12},
{"name":"Kate","age":19},
{"name":"Karl","age":8},
{"name":"Pat","age":11},
{"name":"Pam","age":24}]},
{"name":"Doloris","age":32},
{"name":"Dana","age":23,"children":[
{"name":"Ken","age":12},
{"name":"Kate","age":19},
{"name":"Karl","age":8},
{"name":"Pat","age":11},
{"name":"Pam","age":24}]}]}]}]   

这篇关于带有不规则节点的分层data.frame到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆