Turning relationship data into hierarchical list in R
<p>This is my first question; so please, be gentle.</p>

<p>I have some data that is in the form of :</p>

<pre><code>library('networkD3')
    Relationships&lt;- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
                  Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
&gt; Relationships
  Parent         Child
1  earth         ocean
2  earth        forest
3 forest          tree
4 forest     sasquatch
5  ocean          fish
6  ocean       seaweed
7  ocean mantis shrimp
8  ocean   sea monster
</code></pre>

<p>Essentially this is a list of edges that can be used to make a network map:</p>

<pre><code>net &lt;- graph_from_data_frame(d = Relationships,
                             directed = T)
plot(net)
</code></pre>

<p><a href="https://i.stack.imgur.com/vkzpc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vkzpc.png" alt="enter image description here"></a></p>

<p>I would like to convert it to a form that could be used in the <code>diagonalNetwork</code> function, below.</p>

<pre><code>Hierarchical_list &lt;- list(name = "earth",
                 children = list(list(name = "ocean",
                                      children = list(list(name = "mantis shrimp"),
                                                      list(name = "fish"),
                                                      list(name = "sea monster"),
                                                      list(name = "seaweed")
                                                      )),
                                 list(name = "forest",
                                      children = list(list(name = "sasquatch"),
                                                      list(name = "tree")
                                                      ))
                 ))
diagonalNetwork(Hierarchical_list)
</code></pre>

<p>Like this:</p>

<p><a href="https://i.stack.imgur.com/VkDYg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VkDYg.jpg" alt="enter image description here"></a></p>

<p>When I try to generate the list using this loop:</p>

<pre><code>    List_attempt &lt;- list()

levels&lt;- levels(factor(Relationships$Parent))

for(n in 1:length(levels)){
  Children &lt;- subset(Relationships, Relationships$Parent == levels[n], select = Child)
  for(c in 1:length(Children)){
    sublist &lt;- as.list(Children)
    List_attempt &lt;- list(List_attempt, name = levels[n],children = sublist)
  }
}

diagonalNetwork(List_attempt)
</code></pre>

<p>I get this error:</p>

<pre><code>Error in FUN(X[[i]], ...) : 
  'options' must be a fully named list, or have no names (NULL)
</code></pre>

<p>1) Is there a better way of creating a list for <code>diagonalNetwork</code>? </p>

<p>2) Failing that; How can I modify my loops to kick out a list of the correct structure?</p>

<p>3) Is there a whole other function/package I should be using?</p>

<p>Thanks for any help you can give, I have been beating my head against this wall for a while. Feedback on better ways to ask questions on SO would also be welcomed.</p>

<p>Clarification:</p>

<p>A similar question is found here, <a href="https://stackoverflow.com/questions/30734572/convert-a-data-frame-to-a-treenetwork-compatible-list">Convert a data frame to a treeNetwork compatible list</a>. However it relies on a data structure where the root is always in the first column, and its children are in subsequent columns, not a list of edges as in this question, which is commonly used in igraph. </p>