title: how to replace values in every nth column with value from adjoining column
body: <p>I have a data frame (m2) with 1127 columns and 314 rows. Starting at column 1, I need to replace the values in every 5th column with the 1st value in the adjoining column.  I have been able to do this for individual columns selecting by column name as here: </p>
  
  <pre><code>m2$Col1&lt;-m2[1,2]
  m2$Col6&lt;-m2[1,7]
  m2$Col11&lt;-m2[1,12]
  </code></pre>
  
  <p>or by a very clunky reassign/recombine:</p>
  
  <pre><code>newcol1&lt;-replace(m2[,1],1:314,m2[1,2])
  m2&lt;-cbind(newcol1,m2[,2:1127])
  newcol2&lt;-replace(m2[,6],1:314,m2[1,7])
  m2&lt;-cbind(m2[,1:5],newcol2,m2[,7:1127])
  newcol3&lt;-replace(m2[,11],1:314,m2[1,12])
  m2&lt;-cbind(m2[,1:10],newcol3,m2[,12:1127])
  </code></pre>
  
  <p>I am hoping to find a way to automate this for the data frame.  I am not yet literate in for loops. Can something like this be done with a function?  </p>