title: Convert R dataframe into tough JSON list of lists for d3.hierarchy model
body: <p><strong>Edit: I have cleaned up a bit the question posting, and added a bounty. I will be afk for a few days, but getting this resolved would be a huge help</strong> </p>
  
  <p>I would like to create using d3 a d3.hierarchy of a tree model, using basketball data. I essentially want to create a bracket structured as such:</p>
  
  <p><a href="https://i.stack.imgur.com/y0wk3.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y0wk3.gif" alt="enter image description here"></a></p>
  
  <p>...where the graph / model is a tree where each node has exactly two children (except for all of the end / leaf nodes, of course). This is a textbook example of when you'd want to use the d3.tree() and d3.hierarchy() functionalities, but it requires a JSON in a fairly specific format for the d3.hierarchy command. In particular, for a bracket of 8 basketball teams in a tournament that goes 8 - 4 - 2 - 1, the JSON data needs to be formatted like this:</p>
  
  <pre><code>const playoffData = {
    "name": "Rockets",
    "round": 4,
    "id": 15,
    "children": [
      { 
        "name": "Rockets",
        "round": 3,
        "id": 14,
        "children": [
          { 
            "name": "Rockets",
            "round": 2,
            "id": 9,
            "children": [
              { 
                "name": "Rockets", 
                "round": 1, 
                "id": 1 
              },
              { 
                "name": "Timberwolves", 
                "round": 1,
                "id": 8 
              }
            ]
          },
          { 
            "name": "Jazz",
            "round": 2,
            "id": 12,
            "children": [
              { 
                "name": "Jazz", 
                "round": 1, 
                "id": 4 
              },
              { 
                "name": "Thunder",
                "round": 1, 
                "id": 5
              }
            ]
          }
        ]
      },
      { 
        "name": "Warriors",
        "round": 3, 
        "id": 13,
        "children": [
          { 
            "name": "Warriors",
            "round": 2, 
            "id": 10,
            "children": [
              { 
                "name": "Warriors", 
                "round": 1, 
                "id": 2 
              },
              {
                "name": "Spurs", 
                "round": 1, 
                "id": 7
              }
            ]
          },
          { 
            "name": "Pelicans",
            "round": 2, 
            "id": 11,
            "children": [
              { 
                "name": "Pelicans",
                "round": 1,
                "id": 3
              },
              { 
                "name": "Trail Blazers",
                "round": 1, 
                "id": 6
              }
            ]
          }
        ]
      }
    ]
  };
  </code></pre>
  
  <p>Note the nested nature of the JSONs. The <strong>root</strong> node corresponds with the winner of the bracket, and <strong>leaf</strong> nodes correspond to teams in the first round of the bracket. </p>
  
  <p>I have the following R dataframe of basketball data for the bracket:</p>
  
  <pre><code>&gt; dput(mydata)
  structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
  13, 14, 15), teamname = c("Rockets", "Warriors", "Trail Blazers", 
  "Jazz", "Thunder", "Pelicans", "Spurs", "Timberwolves", "Rockets", 
  "Warriors", "Pelicans", "Jazz", "Rockets", "Warriors", "Rockets"
  ), conference = c("West", "West", "West", "West", "West", "West", 
  "West", "West", "West", "West", "West", "West", "West", "West", 
  "West"), seeding = c(1, 2, 3, 4, 5, 6, 7, 8, NA, NA, NA, NA, 
  NA, NA, NA), round = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 
  3, 4), child1 = c(NA, NA, NA, NA, NA, NA, NA, NA, 1, 2, 3, 4, 
  9, 11, 13), child2 = c(NA, NA, NA, NA, NA, NA, NA, NA, 8, 7, 
  6, 5, 12, 10, 14), wins = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0), losses = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0), completed = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
  FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
  ), winprobs = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
  NA, NA, NA)), .Names = c("id", "teamname", "conference", "seeding", 
  "round", "child1", "child2", "wins", "losses", "completed", "winprobs"
  ), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 17L, 18L, 19L, 
  20L, 25L, 26L, 29L), class = "data.frame")
  
  
  &gt; mydata
  &gt; playoff.data
  
  
     id      teamname conference seeding round child1 child2 wins losses completed winprobs
  1   1       Rockets       West       1     1     NA     NA    0      0     FALSE       NA
  2   2      Warriors       West       2     1     NA     NA    0      0     FALSE       NA
  3   3 Trail Blazers       West       3     1     NA     NA    0      0     FALSE       NA
  4   4          Jazz       West       4     1     NA     NA    0      0     FALSE       NA
  5   5       Thunder       West       5     1     NA     NA    0      0     FALSE       NA
  6   6      Pelicans       West       6     1     NA     NA    0      0     FALSE       NA
  7   7         Spurs       West       7     1     NA     NA    0      0     FALSE       NA
  8   8  Timberwolves       West       8     1     NA     NA    0      0     FALSE       NA
  17  9       Rockets       West      NA     2      1      8    0      0     FALSE       NA
  18 10      Warriors       West      NA     2      2      7    0      0     FALSE       NA
  19 11      Pelicans       West      NA     2      3      6    0      0     FALSE       NA
  20 12          Jazz       West      NA     2      4      5    0      0     FALSE       NA
  25 13       Rockets       West      NA     3      9     12    0      0     FALSE       NA
  26 14      Warriors       West      NA     3     11     10    0      0     FALSE       NA
  29 15       Rockets       West      NA     4     13     14    0      0     FALSE       NA
  </code></pre>
  
  <p>If you can tell, <strong>My R Dataframe</strong> has a row for what will be each node in my d3 graph. Notice the tree structure in particular, and the child1 and child2 helper columns for identifying children - for the Final Round (row 15), its child nodes are the two nodes in the previous round (13 and 14). For row 13 (the semi finals), its children nodes are 9 and 12, etc. The first 8 rows are the first round, and therefore these are leaf nodes and have no children.</p>
  
  <p>Its a bit long, but I wanted to include the whole JSON and R dataframe to keep things clear. I would also like other dataframe columns (wins, losses, win probs) included in the JSON structure, however for a bit of brevity, I did not show these in the JSON above. </p>
  
  <p><strong>A last note:</strong> while I work mainly in R, this is a d3 graph I am making, and as such there is quite a bit of javascript coding that I must do for this. My opinion is that R is better for this type of data manip, however since this is a nested JSON object we're dealing with, maybe JS is better. If there's an eas(ier) solution that involves using javasript to map a 2D JSON version of the R dataframe into the desired nested JSON, that would probably be sufficient as well. </p>
  
  <p>Any help with this is appreciated! I promise to select a top answer once I return to award the bounty.</p>