title: Iteratively and hierarchically cycle through rows till a condition is met
body: <p>I'm trying to solve a data management problem in R.</p>
  
  <p>Suppose my data looks as follows:</p>
  
  <pre><code>id &lt;- c("123", "414", "606")
  next.up &lt;- c("414", "606", "119")
  is.cond.met &lt;- as.factor(c("FALSE", "FALSE", "TRUE"))
  df &lt;- data.frame(id, next.up, is.cond.met)
  
  &gt; df
     id next.up is.cond.met
  1 123     414       FALSE
  2 414     606       FALSE
  3 606     119        TRUE
  </code></pre>
  
  <p><br>
  And I'd like to obtain is the following:</p>
  
  <pre><code>id &lt;- c("123", "414", "606")
  next.up &lt;- c("414", "606", "119")
  is.cond.met &lt;- as.factor(c("FALSE", "FALSE", "TRUE"))
  origin &lt;- c("606", "606", "119")
  df.result &lt;- data.frame(id, next.up, is.cond.met, origin)
  
  &gt; df.result
     id next.up is.cond.met origin
  1 123     414       FALSE    606
  2 414     606       FALSE    606
  3 606     119        TRUE    119
  </code></pre>
  
  <p><br>
  In other words: I want to match each ID to its "origin" when a given condition (is.met) is true. The difficulty I'm having is that this is iterative and hierarchical: to find the origin I may have to go through multiple degrees of separations. the logical steps are illustrated below. I'm really not sure how to tackle this in R.</p>
  
  <p><a href="https://i.stack.imgur.com/zekug.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zekug.png" alt="logical steps"></a></p>
  
  <p><br></p>
  
  <p><strong>UPDATE</strong><br>
  One of the comments propose a data.frame solution which works for sorted data, as in the minimal example above.  In truth, my data is not sorted in such a manner. A better example is as follows:</p>
  
  <pre><code>id &lt;- c("961980", "14788", "902460", "900748", "728912", "141726", "1041190", "692268")
  next.up &lt;- c("20090", "655036", "40375164", "40031850", "40368996", "961980", "141726", "760112")
  is.cond.met &lt;- c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)
  df &lt;- data.frame(id, next.up, is.cond.met, stringsAsFactors = FALSE)
  
  glimpse(df)
  
  Observations: 8
  Variables: 3
  $ id          &lt;chr&gt; "961980", "14788", "902460", "900748", "728912", "141726", "1041190", "692268"
  $ next.up     &lt;chr&gt; "20090", "655036", "40375164", "40031850", "40368996", "961980", "141726", "760112"
  $ is.cond.met &lt;lgl&gt; TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
  &gt; df
         id  next.up is.cond.met
  1  961980    20090        TRUE
  2   14788   655036       FALSE
  3  902460 40375164       FALSE
  4  900748 40031850       FALSE
  5  728912 40368996       FALSE
  6  141726   961980       FALSE
  7 1041190   141726       FALSE
  8  692268   760112       FALSE
  </code></pre>
  
  <p><br></p>
  
  <p><strong>UPDATE 2</strong>: <em>the end result should look like this:</em></p>
  
  <pre><code>&gt; df.end.result
         id  next.up is.cond.met origin
  1  961980    20090        TRUE   &lt;NA&gt;
  2   14788   655036       FALSE   &lt;NA&gt;
  3  902460 40375164       FALSE   &lt;NA&gt;
  4  900748 40031850       FALSE   &lt;NA&gt;
  5  728912 40368996       FALSE   &lt;NA&gt;
  6  141726   961980       FALSE 961980
  7 1041190   141726       FALSE 961980
  8  692268   760112       FALSE   &lt;NA&gt;
  </code></pre>