title: How to count the levels of hierarchy in R?
body: <p>I have a panel data which contains 130 companies' departments in 15 years. The data includes the edge lists of these companies. How do I count every companies' hierarchical levels and identify every department's level in the hierarchy in R?</p>
  
  <pre><code>    source  target
  1   A       B
  2   A       C
  3   A       D
  4   B       E
  5   B       F
  6   C       G
  7   D       H
  8   D       I
  9   D       J
  10  G       K
  </code></pre>
  
  <p>In this case, the top is A, and the bottom is K. The company's hierarchical level is 4. I hope I can create two variables in the panel data of the departments. Like this:</p>
  
  <pre><code>    year    compname    department  complevel   departlevel
  1   2000    Ben Corp.   A           4           1
  2   2000    Ben Corp.   B           4           2
  3   2000    Ben Corp.   C           4           2
  4   2000    Ben Corp.   D           4           2
  5   2000    Ben Corp.   E           4           3
  6   2000    Ben Corp.   F           4           3
  7   2000    Ben Corp.   G           4           3
  8   2000    Ben Corp.   H           4           3
  9   2000    Ben Corp.   I           4           3
  10  2000    Ben Corp.   J           4           3
  11  2000    Ben Corp.   K           4           4
  </code></pre>