title: what is the appropriate way to edit two different objects based on if statement?
body: <p>I want to edit a column of a df only if a condition is met by a second column and also keep track of when the condition is false. What is the appropriate way to do this? </p>
  
  <p>I have tried using <code>ifelse()</code> but both the true and false code is always executed. Plus, it seems like the wrong approach since I do not want the <code>ifelse</code> vector output. (Here I only want to evaluate the first row of each ID).</p>
  
  <pre><code>df &lt;- data.frame(id = rep(c(1,2,3), each = 2), value = rep(c(4,7,-8), 
  each = 2), new_val = rep(0, each=6))
  
  neg_val &lt;- vector()
  
  ifelse(df[!duplicated(df$id), "value"] &gt; 0,
      df[!duplicated(df$id), "new_val"] &lt;- df[!duplicated(df$id),"value"], 
      neg_val &lt;- append(neg_val, df[!duplicated(df$id), "id"]))
  
  [1] 4 7 3
  &gt; neg_val
  [1] 1 2 3
  </code></pre>
  
  <p>The output of the <code>ifelse</code> is as I expected, the positive values or the ID of negative values. But the <code>df</code> is updated for all values, not just when <code>value &gt; 0</code> and <code>neg_val</code> includes all the IDs when I only wanted the IDs for <code>values =&lt; 0</code>. Desired output:</p>
  
  <pre><code>&gt; df
    id value new_val
  1  1     4       4
  2  1     4       0
  3  2     7       7
  4  2     7       0
  5  3    -8       0
  6  3    -8       0
  &gt; neg_val
  [1] 3
  </code></pre>