title: loop to paste columns together if condition is met
body: <p>I would like to create a loop that will create a new column, then paste together two columns if a condition is met in a separate column. If the condition is not met, then the column would equal whatever value is in the existing column. Finally, I would like to delete the old columns and rename the new columns to match the old columns. In my example below, I create columns called a1_t, a2_t, a3_t. Then, if a1 == A, paste a1 and a1_c together and place the value in a1_t, otherwise copy the value from a1 into a1_t. Repeat this procedure for a2_t and a3_t.</p>
  
  <p>Here is the data:</p>
  
  <pre><code>set.seed(1)
  dat &lt;- data.frame(a1 = sample(LETTERS[1:9],15,replace=T),
                    a1_c = sample (1:100,15),
                    a2 = sample(LETTERS[1:9],15,replace=T),
                    a2_c = sample (1:100,15),
                    a3 = sample(LETTERS[1:9],15,replace=T),
                    a3_c = sample (1:100,15))
  </code></pre>
  
  <p>Here is the long hand way of creating my end goal:</p>
  
  <pre><code>dat$a1_t &lt;- 'none'
  dat$a1_t[dat$a1=="A"] &lt;- paste((dat$a1[dat$a1=="A"]),(dat$a1_c[dat$a1=="A"]),sep="_")
  dat$a1_t[dat$a1=="B"] &lt;- 'B'
  dat$a1_t[dat$a1=="C"] &lt;- 'C'
  dat$a1_t[dat$a1=="D"] &lt;- 'D'
  dat$a1_t[dat$a1=="E"] &lt;- 'E'
  dat$a1_t[dat$a1=="F"] &lt;- 'F'
  dat$a1_t[dat$a1=="G"] &lt;- 'G'
  dat$a1_t[dat$a1=="H"] &lt;- 'H'
  dat$a1_t[dat$a1=="I"] &lt;- 'I'
  
  dat$a2_t &lt;- 'none'
  dat$a2_t[dat$a2=="A"] &lt;- paste((dat$a2[dat$a2=="A"]),(dat$a2_c[dat$a2=="A"]),sep="_")
  dat$a2_t[dat$a2=="B"] &lt;- 'B'
  dat$a2_t[dat$a2=="C"] &lt;- 'C'
  dat$a2_t[dat$a2=="D"] &lt;- 'D'
  dat$a2_t[dat$a2=="E"] &lt;- 'E'
  dat$a2_t[dat$a2=="F"] &lt;- 'F'
  dat$a2_t[dat$a2=="G"] &lt;- 'G'
  dat$a2_t[dat$a2=="H"] &lt;- 'H'
  dat$a2_t[dat$a2=="I"] &lt;- 'I'
  
  dat$a3_t &lt;- 'none'
  dat$a3_t[dat$a3=="A"] &lt;- paste((dat$a3[dat$a3=="A"]),(dat$a3_c[dat$a3=="A"]),sep="_")
  dat$a3_t[dat$a3=="B"] &lt;- 'B'
  dat$a3_t[dat$a3=="C"] &lt;- 'C'
  dat$a3_t[dat$a3=="D"] &lt;- 'D'
  dat$a3_t[dat$a3=="E"] &lt;- 'E'
  dat$a3_t[dat$a3=="F"] &lt;- 'F'
  dat$a3_t[dat$a3=="G"] &lt;- 'G'
  dat$a3_t[dat$a3=="H"] &lt;- 'H'
  dat$a3_t[dat$a3=="I"] &lt;- 'I'
  </code></pre>
  
  <p>-al</p>