title: Changing the column name based on a partial string or substring
body: <p>I have a data frame <code>df</code>. I can generate this data frame 5 times for 5 different variables. Let's say variables names are:</p>
  <pre><code>Apple  # apple_df
  Mango  # mango_df
  Banana # banana_df
  Potato # potato_df
  Tomato # tomato_df
  </code></pre>
  <p>Each time the data frame is generated one of the column names is quite large such as:</p>
  <pre><code>Apple - Growth Level Judgement    # Column name for apple_df
  Mango - Growth Level Judgement    # Column name for mango_df
  Banana - Growth Level Judgement   # Column name for banana_df
  Potato - Growth Level Judgement   # Column name for potato_df
  Tomato - Growth Level Judgement   # Column name for tomato_df
  </code></pre>
  <p>I want to change the above column names to just the word <code>Growth</code> across each of the files.</p>
  <p>Is there a way to do it effectively across all data frames by using one common line of code (separately)?</p>
  <p>I can use the complete name in each of the files separately but was wondering if we could have a generalised solution:</p>
  <pre><code># For Apple data frame
  
  # Update column name
  setnames(apple_df, 
           old = c('Apple - Growth Level Judgement'), 
           new = c('Growth'))
  </code></pre>
  <p>If I use the following regex-based solution, it only replaces the part of the string name that is common across all data frames. Unfortunately, not the whole name.</p>
  <pre><code>gsub(x = names(apple_df), 
       pattern = &quot;Growth Level Judgement$&quot;, replacement = &quot;Growth&quot;)  
  
  </code></pre>
  <h4>Related posts:</h4>
  <p>The following post is related but it strips the known part of the string <a href="https://stackoverflow.com/questions/36176287/remove-part-of-column-name">Remove part of column name</a>. In my case, I want to detect the occurrence of a column based on a partial string that stays the same across multiple datasets. But once the string is detected in the column name, I want to change the whole column name. The following posts may also be related but do not meet my needs
  <a href="https://stackoverflow.com/questions/37800704/r-remove-parts-of-column-name-after-certain-characters">r Remove parts of column name after certain characters</a> or <a href="https://stackoverflow.com/questions/46527429/rename-column-names-according-to-pattern-matching-r">Rename column names according to pattern matching R</a></p>
  <p>Any advice on this would be greatly appreciated. Thanks!</p>