Transform a dataframe into a tree structure list of lists

后端 未结 1 1577
忘掉有多难
忘掉有多难 2021-01-05 07:13

I have a data.frame with two columns representing a hierarchical tree, with parents and nodes.

I want to transform its structure in a way that I can use as an input

1条回答
  •  有刺的猬
    2021-01-05 07:35

    Here is a recursive definition:

    maketreelist <- function(df, root = df[1, 1]) {
      if(is.factor(root)) root <- as.character(root)
      r <- list(name = root)
      children = df[df[, 1] == root, 2]
      if(is.factor(children)) children <- as.character(children)
      if(length(children) > 0) {
        r$children <- lapply(children, maketreelist, df = df)
        }
      r
      }
    
    canadalist <- maketreelist(df)
    

    That produces what you desire. This function assumes that the first column of the data.frame (or matrix) you pass in contains the parent column and the second column has the child. it also takes a root parameter which allows you to specify a starting points. It will default to the first parent in the list.

    But if you really are interested in playing round with trees. The igraph package might be of interest

    library(igraph)
    g <- graph.data.frame(df)
    plot(g)
    

    canada tree in igraph

    0 讨论(0)
提交回复
热议问题