title: Trophic position/height in food webs (following paths in networks)
body: <p>As part of developing a demo for a package I'm working on, I need to quantify a classic ecological food web as described below.  I have checked out vegan, bipartite and sna but don't see anything that does what I need, though I could be wrong - those are big packages.  Thus I'm wondering if this idea is already in package, or if someone has a clever way to compute the results.  Seems like it should be a package.</p>
  
  <p>A food web can be described by a matrix of interactions between species A:F, as shown in the code and diagram.  In words, one would say that "A eats B which eats E" etc (very hard to see in the matrix, trivial in the diagram).</p>
  
  <pre><code>species &lt;- LETTERS[1:6]
  
  links &lt;- c(0, 1, 0, 0, 0, 0,
      1, 0, 1, 1, 1, 0,
      0, 1, 0, 0, 1, 0,
      0, 1, 0, 0, 1, 1,
      0, 1, 1, 1, 0, 0,
      0, 0, 0, 1, 0, 0)
  
  L &lt;- matrix(links, nrow = 6, byrow = TRUE,
      dimnames = list(species, species))
  </code></pre>
  
  <p>I want to compute the trophic position and the trophic height of each species.  Trophic position is defined as the total number of species the in the food chain below a particular species + 1.  In the diagram, the trophic position of A is 6 and for D it is 3.  On the other hand, trophic height is the average of the position of a species in each separate chain in which it participates.  Species B is connected to 4 different chains (paths); it's height is the average of the positions considered one at at time: (3 + 3 + 3 + 2)/4 = 2.75.</p>
  
  <p>Computationally, one needs to read the matrix L, then work through the different paths implied by the matrix to compute the needed values.</p>
  
  <p>If this is not too obtuse, does anyone know a package that will do this, or see a way to follow the paths and compute on the various lengths/options?  It "feels" like there must be some recursive/apply approach that should work, but I don't want to reinvent stuff.</p>
  
  <p>Thanks in Advance</p>
  
  <p><img src="https://i.stack.imgur.com/IHbdj.png" alt="a simple food web"></p>