The correct way to recursively create data.frame from a tree

I would like to create a flat data.frameof wood in R.

The tree is represented by a list in which each contains a key with a name childrenthat contains more lists with a large number of children.

tree <-
  list(name="root",
       parent_name='None',
       children=list(
         list(parent_name="root", name="child1", children=list()),
         list(parent_name="root", name="child2", children=list(list(parent_name="child2", name="child3", children=c())))
       )
      )

I would like to โ€œsmoothโ€ this in data.framewith the following structure:

    name parent_name
1   root        None
2 child1        root
3 child2        root
4 child3      child2

I can accomplish this using the following recursive function:

walk_tree <- function(node) {
  results <<- rbind(
    results,
    data.frame(
      name=node$name,
      parent_name=node$parent_name,
      stringsAsFactors=FALSE
    )
  )

  for (node in node$children) {
    walk_tree(node)
  }
}

This function works fine, but requires me to declare results data.frameoutside the function:

results <- NULL
walk_tree(tree)
results # now contains the data.frame as desired

In addition, using an operator <<-triggers the following warning when a function is walk_treeincluded as a function in a package:

Note: no visible binding for '<<-' assignment to 'results'

The use of the operator is <-not performed ( resultsafter walk_tree) is evaluated NULL).

data.frame R?

+4
4

:), dplyr::bind_rows

walk_tree <- function(node) {
  dplyr::bind_rows(
    data.frame(
      name=node$name,
      parent_name=node$parent_name,
      stringsAsFactors=FALSE),
    lapply(node$children,walk_tree)
  )
}

walk_tree(tree)

    name parent_name
1   root        None
2 child1        root
3 child2        root
4 child3      child2

R:

walk_tree <- function(node) {
  do.call(
    rbind,
    c(
    list(data.frame(
      name=node$name,
      parent_name=node$parent_name,
      stringsAsFactors=FALSE)),
    lapply(node$children,walk_tree)
  ))
}

walk_tree(tree)
+1

- "" " " .

#Flatten the nested structure
u_tree <- unlist(tree)

#Gather all the indices where name of the node is equal to parent_name
inds <- grepl("parent_name$", names(u_tree))

#Add them in a dataframe
data.frame(name = u_tree[!inds], parent_name = u_tree[inds])

#    name parent_name
#    root        None
#2 child1        root
#3 child2        root
#4 child3      child2
+3

ape ( (,) , , - "" - (;)).

## Reading a tree
my_tree <- "(child1, (child2, child3));"
tree <- ape::read.tree(text = my_tree)

## Getting the edge table (your flatten format)
tree$edge
#     [,1] [,2]
#[1,]    4    1
#[2,]    4    5
#[3,]    5    2
#[4,]    5    3

Where 4is yours root(the deepest peak in the tree (number of leaves + 1)). It connects "child1"with the top 5. 5denotes the first vertex connecting "child2"and "child3". You can visualize this structure as follows (S3 construction methods for phylo)

## Plotting the tree
plot(tree)
ape::nodelabels()

You can add additional structures (trees) to any child element as follows:

child1_children <- ape::read.tree(text = "(child4, (child5, child6));")
## Adding child1_children to the first leave
tree2 <- ape::bind.tree(tree, child1_children, where = 1)
## Plotting the tree
plot(tree2)
ape::nodelabels()
tree2$edge
#     [,1] [,2]
#[1,]    6    7
#[2,]    7    3
#[3,]    7    8
#[4,]    8    4
#[5,]    8    5
#[6,]    6    9
#[7,]    9    1
#[8,]    9    2

Or delete some using the same principle with ape::drop.tip.

+1
source
rev(data.frame(matrix(stack(tree)[,1],,2,T)))#MHHH seems too easy for the task
      X2     X1
1   None   root
2 child1   root
3 child2   root
4 child3 child2

stack(tree)%>%
mutate(new=rep(1:(n()/2),each=2),ind=rep(ind[2:1],n()/2))%>%
spread(ind,values)
  new   name parent_name
1   1   None        root
2   2 child1        root
3   3 child2        root
4   4 child3      child2
0
source

Source: https://habr.com/ru/post/1693498/


All Articles