title: Rename values in a single column that end with the same string
body: <p>I have several large data frames with one column (let's call it <code>timeperiod</code>) containing text string variables. The variables all end in the same string (<code>V.1to2</code> or <code>V.2to3)</code>, but the beginnings are different.</p>
  <p>I want the values with the same endings changed. As an example:</p>
  <pre><code>df &lt;- data.frame (Location  = c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;),
                     timeperiod = c(&quot;A.V.1to2&quot;, &quot;D.V.1to2&quot;, &quot;A.V.1to2&quot;,&quot;D.V.2to3&quot;,&quot;A.V.3to4&quot;,&quot;H.V.3to4&quot;,&quot;A.V.4to5&quot;,&quot;D.V.4to5&quot;))
  </code></pre>
  <p>As shown:</p>
  <pre><code>  Location timeperiod
  1        a   A.V.1to2
  2        b   D.V.1to2
  3        c   A.V.1to2
  4        d   D.V.2to3
  5        e   A.V.3to4
  6        f   H.V.3to4
  7        g   A.V.4to5
  8        h   D.V.4to5
  </code></pre>
  <p>***(edit) There a few different data frame types and they all need the timeperiod column changed in a way that assigns numbers in place of the strings however, I oversimplified when I first ask this question. In some cases the number matches the first number in the string, but in other cases it does not match. Here is an updated expected outcome that reflects this situation. ***</p>
  <p>My desired outcome would be as follows:</p>
  <pre><code>df2
    Location timeperiod
  1        a          0
  2        b          0
  3        c          0
  4        d          2
  5        e          3
  6        f          3
  7        g          5
  8        h          5
  
  df2 &lt;- data.frame (Location  = c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;),
                    timeperiod = c(0, 0, 0, 2, 3, 3, 5, 5))
  
  
  </code></pre>
  <p><em><strong>(edit) With this expected outcome you can see that there are situations where the numbers match the first number in the string but there are other situations in the data where it doesn't match. May apologies for not making this clear in the first example</strong></em></p>
  <p>Sample code:</p>
  <pre><code>df$timeperiod[df$timeperiod ==&quot;A.V.1to2&quot;] &lt;- &quot;0&quot;
  </code></pre>
  <p>Because of the size of my data set and the need to repeat this for multiple data frames with different prefixes for the time period values, I'd like to use <code>dplyr</code> like this:</p>
  <pre><code>library(dplyr)
  df$timeperiod &lt;- revalue(df$timeperiod, c(ends_with(V.1to2)=&quot;0&quot;))
  df$timeperiod &lt;- revalue(df$timeperiod, c(ends_with(V.2to3)=&quot;2&quot;))
  #etc..
  
  </code></pre>
  <p>So I can do it again with different values and sheets. But this doesn't work, and renaming every specific value seems inefficient, so any solution faster than this would serve its purpose.</p>