title: R conditional replacement data frame values
body: <p>I want to update a dataframe column (df$d) with the result of a mathematical operation on one or more other columns (df$b, df$c), conditional on the value of another column (df$a).</p>
  
  <pre><code>set.seed(55)
  df &lt;- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10), d = 0)
  df$d[df$a &gt; 0] &lt;- df$b[df$a &gt; 0] / df$c[df$a &gt; 0]
  </code></pre>
  
  <p>The third line produces the expected values for df$d. Is there a way to write this more succinctly? I'm especially interested in options that do not require me to repeat the logical index.</p>
  
  <p>For example, this style of expression works in Python/Pandas and requires only one instance of '[df$a > 0]' on the left-hand side of the assignment operator:</p>
  
  <pre><code>df$d[df$a &gt; 0] &lt;- df$b / df$c
  </code></pre>
  
  <p>Thank you for any and all advice.</p>