返回嵌套级别和值的嵌套列表

时间:2016-12-15 05:54:41

标签: r recursion nested-lists networkd3

我想使用 networkD3 来显示一些深层嵌套的数据。在发送给radialNetwork之前,我无法弄清楚如何将数据格式化为正确的格式。

以下是一些示例数据:

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

其中level表示嵌套的级别,value是节点的名称。通过使用这两个向量,我需要将数据转换为以下格式:

my_list <- list(
  name = "root",
  children = list(
    list(
      name = value[1], ## a
      children = list(list(
        name = value[2], ## b
        children = list(list(
          name = value[3], ## c
          children = list(
            list(name = value[4]), ## d
            list(name = value[5]) ## e
          )
        ),
        list(
          name = value[6], ## f
          children = list(
            list(name = value[7]), ## g
            list(name = value[8]) ## h
          )
        ))
      ))
    ),
    list(
      name = value[9], ## i
      children = list(list(
        name = value[10], ## j
        children = list(list(
          name = value[11] ## k
        ))
      ))
    )
  )
)

这是解除对象:

> dput(my_list)
# structure(list(name = "root",
#                children = list(
#                  structure(list(
#                    name = "a",
#                    children = list(structure(
#                      list(name = "b",
#                           children = list(
#                             structure(list(
#                               name = "c", children = list(
#                                 structure(list(name = "d"), .Names = "name"),
#                                 structure(list(name = "e"), .Names = "name")
#                               )
#                             ), .Names = c("name",
#                                           "children")), structure(list(
#                                             name = "f", children = list(
#                                               structure(list(name = "g"), .Names = "name"),
#                                               structure(list(name = "h"), .Names = "name")
#                                             )
#                                           ), .Names = c("name",
#                                                         "children"))
#                           )), .Names = c("name", "children")
#                    ))
#                  ), .Names = c("name",
#                                "children")), structure(list(
#                                  name = "i", children = list(structure(
#                                    list(name = "j", children = list(structure(
#                                      list(name = "k"), .Names = "name"
#                                    ))), .Names = c("name",
#                                                    "children")
#                                  ))
#                                ), .Names = c("name", "children"))
#                )),
#           .Names = c("name",
#                      "children"))

然后我可以将它传递给最终的绘图功能:

library(networkD3)
radialNetwork(List = my_list)

输出看起来类似于:

enter image description here

问题:如何创建嵌套列表?

注意:正如@ zx8754所指出的,此SO post中已有解决方案,但需要data.frame作为输入。由于我level的不一致,我没有看到将其转换为data.frame的简单方法。

2 个答案:

答案 0 :(得分:4)

使用data.table式合并:

library(data.table)
dt = data.table(idx=1:length(value), level, parent=value)

dt = dt[dt[, .(i=idx, level=level-1, child=parent)], on=.(level, idx < i), mult='last']

dt[is.na(parent), parent:= 'root'][, c('idx','level'):= NULL]

> dt
#     parent child
#  1:   root     a
#  2:      a     b
#  3:      b     c
#  4:      c     d
#  5:      c     e
#  6:      b     f
#  7:      f     g
#  8:      f     h
#  9:   root     i
# 10:      i     j
# 11:      j     k

现在我们可以使用其他post的解决方案:

x = maketreelist(as.data.frame(dt))

> identical(x, my_list)
# [1] TRUE

答案 1 :(得分:2)

作为前言,您的数据很难处理,因为关键信息是按level中的值的顺序编码的。我不知道你是如何按照这个顺序获得这些价值的,但是考虑到最初可能有更好的方法来构建这些信息,这将使下一个任务变得更容易。

这里&#39; sa base - 将您的数据转换为包含2列parentchild的数据框,然后将其传递到data.tree个函数可以轻松转换为您需要的JSON格式...然后将其传递给radialNetwork ...

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(data.tree)
library(networkD3)

parent_idx <- sapply(1:length(level), function(n) rev(which(level[1:n] < level[n]))[1])
df <- data.frame(parent = value[parent_idx], child = value, stringsAsFactors = F)
df$parent[is.na(df$parent)] <- ""

list <- ToListExplicit(FromDataFrameNetwork(df), unname = T)
radialNetwork(list)

这是tidyverse实现同样目标的方法......

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(tidyverse)
library(data.tree)
library(networkD3)

data.frame(level, value, stringsAsFactors = F) %>%
  mutate(row = row_number()) %>%
  mutate(level2 = level, value2 = value) %>%
  spread(level2, value2) %>%
  mutate(`0` = "") %>%
  arrange(row) %>%
  fill(-level, -value, -row) %>%
  gather(parent_level, parent, -level, -value, -row) %>%
  filter(parent_level == level - 1) %>%
  arrange(row) %>%
  select(parent, child = value) %>%
  data.tree::FromDataFrameNetwork() %>%
  data.tree::ToListExplicit(unname = TRUE) %>%
  radialNetwork()

对于奖励,当前开发版networkD3(v0.4.9000)有一个新的treeNetwork函数,该数据框采用nodeIdparentId列的数据框/ variables,它消除了data.tree函数转换为JSON的需要,所以像这样的东西......

level <- c(1, 2, 3, 4, 4, 3, 4, 4, 1, 2, 3)
value <- letters[1:11]

library(tidyverse)
library(networkD3)

data.frame(level, value, stringsAsFactors = F) %>%
  mutate(row = row_number()) %>%
  mutate(level2 = level, value2 = value) %>%
  spread(level2, value2) %>%
  mutate(`0` = "root") %>%
  arrange(row) %>%
  fill(-level, -value, -row) %>%
  gather(parent_level, parent, -level, -value, -row) %>%
  filter(parent_level == level - 1) %>%
  arrange(row) %>%
  select(nodeId = value, parentId = parent) %>%
  rbind(data.frame(nodeId = "root", parentId = NA)) %>% 
  mutate(name = nodeId) %>% 
  treeNetwork(direction = "radial")