title: R Replacing colnames based on matching names in another dataframe
body: <p>I want to rename some columns in a dataframe based on another dataframe. I have 2 dataframes, one with codes as columnnames and a seconds one with a list of codes and a string. </p>
  
  <pre><code>df = as.data.frame(matrix(NA, 3, 60)) colnames(df) = c(paste0(sprintf("S%s",seq(10,19))), paste0(sprintf("R%s",seq(20,50))), paste0(sprintf("F%s",seq(51,69))))
  
  namelist = as.data.frame(c(paste0(sprintf("B%s",seq(10,19))), paste0(sprintf("R%s",seq(20,50))), paste0(sprintf("A%s",seq(51,69)))))
  colnames(namelist) = "Code"
  myFun &lt;- function(n = 5000) {
    a &lt;- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
    paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))}
  namelist$Name = myFun(nrow(namelist))
  </code></pre>
  
  <p>I want to rename now the colnames in df, which are listed in the in the namelist. Their new name should be the string standing behind that matching code in the namelist. </p>
  
  <p>The result should be then: Keeping the columnnames that are not listed as a code in namelist and replacing R20-R50 with the string. </p>
  
  <p>I need a solution that is really taking the code in one piece because I also might have combined Codes as columnanmes, like R40+R41, which should not be renamed. </p>
  
  <p>Any idea how to do that?</p>