title: Finding indirect nodes for every edge (in R)
body: <p>I have information on groups of physicians working together in given hospitals. A physician can work in more than one hospital at the same time. I would like to write a code that outputs information of all indirect colleagues of a given physician working in a given hospital. For instance, if I work in a given hospital with another physician who also works in another hospital, I would like to know who are the physicians with whom my colleague works in this other hospital.</p>
  <p>Consider a simple example of three hospitals (1, 2, 3) and five physicians (A, B, C, D, E). Physicians A, B and C work together in hospital 1. Physicians A, B and D work together in hospital 2. Physicians B and E work together in hospital 3.</p>
  <p>For each physician working in a given hospital I would like information of their indirect colleagues through each of their direct colleagues. For example, physician A has one indirect colleague through physician B in hospital 1: this is physician E in hospital 3. On the other hand, physician B does not have any indirect colleague through physician A in hospital 1. Physician C has two indirect colleagues through physician B in hospital 1: they are physician D in hospital 2 and physician E in hospital 3. And so on..</p>
  <p>Below is the object that describes the nertworks of physicians in all hospitals:</p>
  <pre><code>edges &lt;- tibble(hosp  = c(&quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;3&quot;, &quot;3&quot;), 
               from = c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;D&quot;, &quot;D&quot;, &quot;B&quot;, &quot;E&quot;), 
               to   = c(&quot;C&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;D&quot;, &quot;B&quot;, &quot;A&quot;, &quot;D&quot;, &quot;A&quot;, &quot;B&quot;, &quot;E&quot;, &quot;B&quot;)) %&gt;% arrange(hosp, from, to)
  </code></pre>
  <p>I would like a code that produces the following output:</p>
  <pre><code>output &lt;- tibble(hosp     = c(&quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;1&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;, &quot;3&quot;), 
               from     = c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;D&quot;, &quot;D&quot;, &quot;D&quot;, &quot;B&quot;, &quot;E&quot;, &quot;E&quot;, &quot;E&quot;, &quot;E&quot;), 
               to       = c(&quot;C&quot;, &quot;B&quot;, &quot;C&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;D&quot;, &quot;B&quot;, &quot;A&quot;, &quot;D&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;E&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;),
               hosp_ind = c(&quot;&quot; , &quot;3&quot;, &quot;&quot; , &quot;&quot; , &quot;2&quot;, &quot;2&quot;, &quot;3&quot;, &quot;&quot; , &quot;3&quot;, &quot;&quot; , &quot;&quot; , &quot;1&quot;, &quot;1&quot;, &quot;3&quot;, &quot;&quot; , &quot;1&quot;, &quot;1&quot;, &quot;2&quot;, &quot;2&quot;),
               to_ind   = c(&quot;&quot; , &quot;E&quot;, &quot;&quot; , &quot;&quot; , &quot;D&quot;, &quot;D&quot;, &quot;E&quot;, &quot;&quot; , &quot;E&quot;, &quot;&quot; , &quot;&quot; , &quot;C&quot;, &quot;C&quot;, &quot;E&quot;, &quot;&quot; , &quot;A&quot;, &quot;C&quot;, &quot;A&quot;, &quot;D&quot;)) %&gt;% arrange(hosp, from, to)
  </code></pre>