title: Create new conditional string variable in R that is a paste() of existing string variables
body: <p>I have a problem with R that is driving me crazy....</p>
  
  <p>I set a lot of conditional variables based on reported data (cleaning and validation) and run into this all the time. It has to do with the length of replacement vectors in combination with conditional statements (excuse my poor explanation...).</p>
  
  <p>Let me run an example by you:</p>
  
  <p>Based on reported data I create a "synthetic" character variable <code>batch_id</code> as follows <code>paste(var1, var2, sep="")</code>. However, only when <code>var2</code> is 6 characters long AND ends in a "B".</p>
  
  <p>If var2 is shorter (which it often is) or does not end in "B" I want <code>batch_id &lt;- NA</code> (missing)</p>
  
  <p>I tried with the following:</p>
  
  <p><code>data &lt;- within(data, batch_id[nchar(data$var2) &lt;6] &lt;- NA)</code>    </p>
  
  <p><code>data &lt;- within(data, batch_id[nchar(data$var2) == 6 &amp;
           !substr(data$var2, 6, 6) == "B"] &lt;- NA)</code></p>
  
  <p><code>data &lt;- within(
           data, batch_id[nchar(data$var2) == 6 &amp;
           substr(data$var2, 6, 6) == "B"] &lt;- paste(data$var1, data$var2, sep=""))</code></p>
  
  <p>However, on the last line of code I get the error message :</p>
  
  <p><code>number of items to replace is not a multiple of replacement length</code></p>
  
  <p>Oooh, how I love this error message !! :)</p>
  
  <p>I know that by setting the same condition <code>[...]</code> on the right hand side for <strong>both</strong> <code>var1</code> and <code>var2</code> it actually works, but there must be a better (more elegant and more readable later on) way of doing this ??</p>