title: Using `dplyr` or `purrr` to get means of multiple columns that share a string fragment (e.g. year)
body: <p>I have a dataframe that looks like this:</p>
  
  <pre><code>df &lt;- 
    data.frame(
      a_1995 = 1:4,
      b_1995 = 11:14,
      a_1996 = 21:24,
      a_1997 = 1:4,
      b_1997 = 51:54,
      a_1998 = 31:34,
      a_1999 = 21:24)
  </code></pre>
  
  <p>For some years, I have multiple measures, therefore. I want to create a new set of columns, which are the averages of the 1 or 2 measurements take for that year. I could do this manually as follows to get the desired output:</p>
  
  <pre><code>out &lt;- 
    df %&gt;% 
    mutate(
      avg_1995 = rowMeans(select(., contains("1995"))),
      avg_1996 = rowMeans(select(., contains("1996"))),
      avg_1997 = rowMeans(select(., contains("1997"))),
      avg_1998 = rowMeans(select(., contains("1998"))),
      avg_1999 = rowMeans(select(., contains("1999"))))
  </code></pre>
  
  <p>Is there a way to automate this using <code>purrr</code> or <code>dplyr</code> functions? (I have hundreds of columns like this.)  </p>