title: Setting a value in a variable to NA, conditional on another variable
body: <p>I'm looking to remove the value in a variable if the condition of another variable is satisfied. For instance:</p>
  
  <pre><code>df$var1[df$condvar == 0] &lt;- NA
  </code></pre>
  
  <p>The code above works fine, but I need to repeat this for dozens more variables, so <code>var1</code> above would change to <code>var2</code>, <code>var3</code>, etc.. This is always based on the same <code>condvar</code>, although for half of the variables the condition is <code>df$condvar == 1</code>. It's cumbersome to repeat this line over and over again, and I was wondering if there was a more concise way to code this. Would one of the <code>apply</code> functions help, or would I need to create a custom function?</p>
  
  <p>As a reproducible example, I'm looking to avoid the repetitive nature of the code below:</p>
  
  <pre><code>ex &lt;- mtcars
  ex$mpg[ex$vs == 0] &lt;- NA
  ex$disp[ex$vs == 0] &lt;- NA
  ex$drat[ex$vs == 0] &lt;- NA
  ex$cyl[ex$vs == 1] &lt;- NA
  ex$hp[ex$vs == 1] &lt;- NA
  ex$wt[ex$vs == 1] &lt;- NA
  ex
  
  
                       mpg cyl  disp  hp drat    wt  qsec vs am gear carb
  Mazda RX4             NA   6    NA 110   NA 2.620 16.46  0  1    4    4
  Mazda RX4 Wag         NA   6    NA 110   NA 2.875 17.02  0  1    4    4
  Datsun 710          22.8  NA 108.0  NA 3.85    NA 18.61  1  1    4    1
  Hornet 4 Drive      21.4  NA 258.0  NA 3.08    NA 19.44  1  0    3    1
  Hornet Sportabout     NA   8    NA 175   NA 3.440 17.02  0  0    3    2
  Valiant             18.1  NA 225.0  NA 2.76    NA 20.22  1  0    3    1
  Duster 360            NA   8    NA 245   NA 3.570 15.84  0  0    3    4
  etc.
  </code></pre>
  
  <p>I'd be perfectly happy if there's one line of code that applies to all variables for which <code>condvar == 0</code> and another for those variables for which <code>condvar == 1</code>.</p>