title: loop for renaming columns in r
body: <p>I have wide data that I want to transform to long for. But before doing this, I want to rename all the variable column names.</p>
  <p>The first variables in my data frame are anagraphical (id, names, etc) so the loop should run for all columns except the first 9.</p>
  <p>Moreover, being the data is wide, I have repeated variables (that should have the same prefix) in the columns representing different years (10 years).</p>
  <p>I was thinking at something like this:</p>
  <pre><code>for (i in seq(10:440)){
    names(mydata)[i:i+10]&lt;- paste(&quot;varname&quot;, 1:10, sep=&quot;_&quot;)
  }
  </code></pre>
  <p>Obviously, it doesn't work. But I need something like this, with also &quot;varname&quot; varying with i (I need to recode about 45 variables repeated for 10 years).</p>
  <p>my data look like this</p>
  <pre><code>id Operating_renvenue_last_yr Operating_renvenue_-1 Operating_renvenue-2 ... Fixed_assets_last_yr Fixed_assets-1 Fixed_assets-_2 
  ESA08005449 1973859 1983692 2028124 ... 205824 205955 208695
  ESA08000820 1044971 962639 912788 ... 100355 120558 135448
  ESA17000852 1005575 1035578 1055304 ... 509555 520687 705777
  ESA08800450 861971 812596 765714 ... 1120587 1130458 1145200
  </code></pre>
  <p>And I want to obtain:</p>
  <pre><code>id            OR_1    OR_2     OR_3 ... FA_1    FA_2   FA_3 
  ESA08005449 1973859 1983692 2028124 ... 205824 205955 208695
  ESA08000820 1044971 962639 912788 ... 100355 120558 135448
  ESA17000852 1005575 1035578 1055304 ... 509555 520687 705777
  ESA08800450 861971 812596 765714 ... 1120587 1130458 1145200
  </code></pre>