title: Replace column names with extracted strings
body: <p>I'm trying to replace some of the columns in my data frame with extracted strings from each column name. This is my current data frame:</p>
  <pre><code>  Date      Time   Temp  ActivityLevelActivity  ExplainActivityvalues4  AppetiteLevelAppetite
    10/22/21  10:26  76    4                      Activity was low        8
    10/23/21  8:42   79    3                      Activity was low again  7
  </code></pre>
  <p>I would like to replace the &quot;ActivityLevelActivity&quot; and &quot;AppetiteLevelAppetite&quot; column names with just &quot;Activity&quot; and &quot;Appetite&quot;. I would like to change the &quot;ExplainActivityvalues4&quot; to &quot;Activity_Comments&quot;.</p>
  <p>I have tried:</p>
  <pre><code>gsub(&quot;Level&quot;, &quot;[^L]+&quot;, names(df))
  gsub(&quot;Explain&quot;, &quot;(?&lt;=\\n)[[:alpha:]]+(?&lt;=\\v)&quot;, names(df))
  </code></pre>
  <p>I used &quot;Level&quot; and &quot;Explain&quot; as the patterns because the word &quot;Level&quot; is included in every column name where I would just like to take the first word. &quot;Explain&quot; is included for every column name where I would like to take the middle word and add &quot;_Comments&quot;.</p>
  <p>Essentially, I would like the new data frame to look like this:</p>
  <pre><code>Date      Time   Temp  Activity  Activity_Comments       Appetite
  10/22/21  10:26  76    4         Activity was low        8
  10/23/21  8:42   79    3         Activity was low again  7
  </code></pre>
  <p>EDIT:</p>
  <p>To explain further, here are all of my column names:</p>
  <pre><code>names(df) &lt;- c(“Date”, “Time”, “Temp”, “ActivityLevelActivity”, “ExplainActivityvalues4”, “AppetiteLevelAppetite”, “ExplainAppetitevalues4”, “ComfortLevelComfort”, “ExplainComfortvalues4”, “DemeanorLevelDemeanor”, “ExplainDemeanorvalues4”, CooperationLevelCooperation”, “ExplainCooperationvalues4”, “HygieneLevelHygiene”, “ExplainHygienevalues4”, “MobilityLevelMobility”, “ExplainMobilityvalues4”)
  </code></pre>