title: How to rename data frame variables as x1, x2, ... in R?
body: <p>I know there's a simple solution, but I keep getting error messages. Given this sample data frame:</p>
  
  <pre><code>y &lt;- c(1,2,3)
  a &lt;- c(2,4,6)
  b &lt;- c(4,8,12)
  c &lt;- c(8,16,24)
  z &lt;- as.data.frame(cbind(y,a,b,c))
  z
    y a  b  c
  1 1 2  4  8
  2 2 4  8 16
  3 3 6 12 24
  </code></pre>
  
  <p>I'd like an automated piece of code that renames column 2 to x1, column 3 to x2, and column 3 to x3. I've attempted:</p>
  
  <pre><code>for (i in 2:4){ x=colnames(z[i]); rename(z, c(x=paste("x",i,sep=""))) }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>apply(z[,2:4], 2, function(x) rename(z, c(x=paste("x",i,sep="")))  )
  </code></pre>
  
  <p>but no success. Any help is much appreciated!</p>