title: Convert a data frame to a treeNetwork compatible list
body: <p>Consider the following data frame:</p>
  
  <pre><code>   Country     Provinces          City Zone
  1   Canada   Newfondland      St Johns    A
  2   Canada           PEI Charlottetown    B
  3   Canada   Nova Scotia       Halifax    C
  4   Canada New Brunswick   Fredericton    D
  5   Canada        Quebec            NA   NA
  6   Canada        Quebec   Quebec City   NA
  7   Canada       Ontario       Toronto    A
  8   Canada       Ontario        Ottawa    B
  9   Canada      Manitoba      Winnipeg    C
  10  Canada  Saskatchewan        Regina    D
  </code></pre>
  
  <p>Would there be a clever way to convert it to a <code>treeNetwork</code> compatible list (from the <code>networkD3</code> package) in the form of:</p>
  
  <pre><code>CanadaPC &lt;- list(name = "Canada",
                   children = list(
                     list(name = "Newfoundland",
                          children = list(list(name = "St. John's",
                                               children = list(list(name = "A"))))),
                     list(name = "PEI",
                          children = list(list(name = "Charlottetown",
                                               children = list(list(name = "B"))))),
                     list(name = "Nova Scotia",
                          children = list(list(name = "Halifax",
                                               children = list(list(name = "C"))))),
                     list(name = "New Brunswick",
                          children = list(list(name = "Fredericton",
                                               children = list(list(name = "D"))))),
                     list(name = "Quebec",
                          children = list(list(name = "Quebec City"))),
                     list(name = "Ontario",
                          children = list(list(name = "Toronto",
                                               children = list(list(name = "A"))),
                                          list(name = "Ottawa",
                                               children = list(list(name = "B"))))),
                     list(name = "Manitoba",
                          children = list(list(name = "Winnipeg",
                                               children = list(list(name = "C"))))),
                     list(name = "Saskatchewan",
                          children = list(list(name = "Regina",
                                               children = list(list(name = "D")))))))
  </code></pre>
  
  <p>In order to plot a <em>Reingold-Tilford</em> tree that would have an arbitrary set of levels:</p>
  
  <p><img src="https://i.stack.imgur.com/ntJc1.jpg" alt="enter image description here"></p>
  
  <p>I have tried several sub-optimal routines including a messy combination of <code>for</code> loops but I can't get this in the desired format. </p>
  
  <p>Ideally, the function would scale in order to consider the first column as the <code>root</code> (starting point) and the other columns would be different levels of children.</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>A <a href="https://stackoverflow.com/questions/23839142/transforming-a-data-frame-into-a-tree-structure-list-of-lists-in-r">similar question</a> was asked on the same topic and @MrFlick provided an interesting recursive function. The original data frame had a fixed set of levels. I introduced <code>NA</code>s to add another level of complexity (arbitrary set of levels) that is not adressed in @MrFlick initial solution.</p>
  
  <hr>
  
  <p><strong>Data</strong></p>
  
  <pre><code>structure(list(Country = c("Canada", "Canada", "Canada", "Canada", 
  "Canada", "Canada", "Canada", "Canada", "Canada", "Canada"), 
      Provinces = c("Newfondland", "PEI", "Nova Scotia", "New Brunswick", 
      "Quebec", "Quebec", "Ontario", "Ontario", "Manitoba", "Saskatchewan"
      ), City = c("St Johns", "Charlottetown", "Halifax", "Fredericton", 
      NA, "Quebec City", "Toronto", "Ottawa", "Winnipeg", "Regina"
      ), Zone = c("A", "B", "C", "D", NA, NA, "A", "B", "C", 
      "D")), class = "data.frame", row.names = c(NA, -10L), .Names = c("Country", 
  "Provinces", "City", "Zone"))
  </code></pre>