title: R: Looping through string variable
body: <p>I have a tedious task, that I would like to outsource to R. I thought about a loop, that does the same for every wave ( "99" to "16"). Basically, whenever there is a 99 in the code below, I would like to adapt this for every wave. </p>
  
  <p>I am used to STATA, where I would simply to this with replacing 99 with <strong>`wave'</strong> and use a foreach loop. Is there a simple way to do this in R? How can I reference a string in a loop? </p>
  
  <p>Otherwise it might just be a "copy-paste"-task :-) Thanks a lot for your help!</p>
  
  <pre><code>`wave &lt;- ("99", "00", "01", "02", "03", "04")`
  
  
  
  names(m99) &lt;- gsub("(^x99)|(^h99)|(^i99)|(^weih99)", "", names(m99))
  names(m99) &lt;- gsub("99$", "", names(m99))
  names(m99) &lt;- paste(names(m99), "h", sep="_")
  m99$idhous &lt;- m99$idhous_h 
  
  p99 &lt;- list_files_p[[1]]
  names(p99)
  names(p99) &lt;- gsub("(^p99)|(^i99)|(^weip99)","", names(p99))
  names(list_files_p[[1]]) &lt;- gsub("99$","", names(list_files_p[[1]]))
  p99[,grep("^x",names(p99))] &lt;-  NULL
  names(p99) &lt;- paste(names(p99), "p", sep="_")
  names(p99)
  sort(table(names(p99)))
  p99$idhous &lt;- p99$idhous_p
  
  pm99 &lt;- left_join(p99, m99, by="idhous")
  </code></pre>