title: R change column names over multiple data
body: <p>I'm trying to change column names over multiple data sets. I have tried writing the following function to do this:</p>
  
  <pre><code># simplified test data #
  df1&lt;-as.data.frame(c("M","F"))
  colnames(df1)&lt;-"M1"
  
  # my function #
  rename_cols&lt;-function(df){
    colnames(df)[names(df) == "M1"] &lt;- "sex"
  }
  rename_cols(df1)
  </code></pre>
  
  <p>However when testing this function on df1, the column is always called "M1" instead of "sex". How can I correct this?</p>
  
  <p>SOLUTION - THANKS TO DAVID ARENBERG</p>
  
  <pre><code>rename_cols&lt;-function(df){
    colnames(df)[names(df) == "M1"] &lt;- "sex"
    df
  }
  df1&lt;-rename_cols(df1)
  </code></pre>