title: Transform a dataframe into a tree structure list of lists
body: <p>I have a data.frame with two columns representing a hierarchical tree, with parents and nodes.</p>
  
  <p>I want to transform its structure in a way that I can use as an input for the function <code>d3tree</code>, from <code>d3Network</code> package.</p>
  
  <p>Here's my data frame:</p>
  
  <pre><code>df &lt;- data.frame(c("Canada","Canada","Quebec","Quebec","Ontario","Ontario"),c("Quebec","Ontario","Montreal","Quebec City","Toronto","Ottawa"))
  names(df) &lt;- c("parent","child")
  </code></pre>
  
  <p>And I want to transform it to this structure</p>
  
  <pre><code>Canada_tree &lt;- list(name = "Canada", children = list(
                                                  list(name = "Quebec", 
                  children = list(list(name = "Montreal"),list(name = "Quebec City"))),
                                                   list(name = "Ontario", 
                  children = list(list(name = "Toronto"),list(name = "Ottawa")))))
  </code></pre>
  
  <p>I have succesfully transformed this particular case using this code below:</p>
  
  <pre><code>fill_list &lt;- function(df,node) node &lt;- as.character(node)if (is.leaf(df,node)==TRUE){
      return (list(name = node))
    }
    else {
      new_node = df[df[,1] == node,2]
  
      return (list(name = node, children =  list(fill_list(df,new_node[1]),fill_list(df,new_node[2]))))
    }
  </code></pre>
  
  <p>The problem is, it only works with trees which every parent node has exactly two children.
  You can see I hard coded the two children (new_node[1] and new_node[2]) as inputs for my recursive function.</p>
  
  <p>I'm trying to figure out a way that I could call the recursive function as many time as the parent's node children. 
  Example: </p>
  
  <pre><code>fill_list(df,new_node[1]),...,fill_list(df,new_node[length(new_node)])
  </code></pre>
  
  <p>I tried these 3 possibilities but none of it worked:</p>
  
  <p><strong>First:</strong> Creating a string with all the functions and parameters and then evaluating. It return this error <code>could not find function fill_functional(df,new_node[1])</code>. That's because my function wasn´t created by the time I called it after all.</p>
  
  <pre><code>fill_functional &lt;- function(df,node) {
    node &lt;- as.character(node)
    if (is.leaf(df,node)==TRUE){
      return (list(name = node))
    }
    else {
      new_node = df[df[,1] == node,2]
      level &lt;- length(new_node)
      xxx &lt;- paste0("(df,new_node[",seq(level),"])")
      lapply(xxx,function(x) eval(call(paste("fill_functional",x,sep=""))))
  
    }
  }
  </code></pre>
  
  <p><strong>Second:</strong> Using a for loop. But I only got the children of my root node.</p>
  
  <pre><code>L &lt;- list()
  fill_list &lt;- function(df,node) {
    node &lt;- as.character(node)
    if (is.leaf(df,node)==TRUE){
      return (list(name = node))
    }
    else {
      new_node = df[df[,1] == node,2]
  
      for (i in 1:length(new_node)){
        L[i] &lt;- (fill_list(df,new_node[i]))
      }
  
      return (list(name = node, children = L))
    }
  }
  </code></pre>
  
  <p><strong>Third:</strong> Creating a function that populates a list with elements that are functions, and just changing the arguments. But I wasn't able to accomplish anything interesting, and I'm afraid I'll have the same problem as I did on my first try described above.</p>