title: Conditional replacement of values in a data.frame
body: <p>I am trying to understand how to conditional replace values in a dataframe without using a loop. My data frame is structured as follows:</p>
  
  <pre><code>&gt; df
            a b est
  1  11.77000 2   0
  2  10.90000 3   0
  3  10.32000 2   0
  4  10.96000 0   0
  5   9.90600 0   0
  6  10.70000 0   0
  7  11.43000 1   0
  8  11.41000 2   0
  9  10.48512 4   0
  10 11.19000 0   0
  </code></pre>
  
  <p>and the <code>dput</code> output is this:</p>
  
  <pre><code>structure(list(a = c(11.77, 10.9, 10.32, 10.96, 9.906, 10.7, 
  11.43, 11.41, 10.48512, 11.19), b = c(2, 3, 2, 0, 0, 0, 1, 2, 
  4, 0), est = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("a", 
  "b", "est"), row.names = c(NA, -10L), class = "data.frame")
  </code></pre>
  
  <p>What I want to do, is to check the value of <code>b</code>. If <code>b</code> is 0, I want to set <code>est</code> to a value from <code>a</code>. I understand that <code>df$est[df$b == 0] &lt;- 23</code> will set all values of <code>est</code> to 23, when <code>b==0</code>. What I don't understand is how to set <code>est</code> to a value of <code>a</code> when that condition is true. For example:</p>
  
  <pre><code>df$est[df$b == 0] &lt;- (df$a - 5)/2.533 
  </code></pre>
  
  <p>gives the following warning:</p>
  
  <pre><code>Warning message:
  In df$est[df$b == 0] &lt;- (df$a - 5)/2.533 :
    number of items to replace is not a multiple of replacement length
  </code></pre>
  
  <p>Is there a way that I can pass the relevant cell, rather than vector?</p>