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
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)