title: How to extract substring as part of dplyr::mutate piping
body: <p>I have the following data frame:</p>
  
  <p>
  <br/></p>
  
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  
  df &lt;-  structure(list(pfc_chr = c("chr1", "chr1", "chr1", "chr1", "chr1", 
  "chr1", "chr1", "chr1", "chr1", "chr1"), pfc_chr_st = c(3046442L, 
  3119671L, 3164756L, 3167322L, 3210838L, 3212196L, 3249068L, 3268246L, 
  3444892L, 3451544L), peak_name = c("XXX-ad_peak_1", "XXX-ad_peak_2a", 
  "PMN_peak_2", "Ytb_peak_3", "PMN_peak_3", "XXX-ad_peak_6", 
  "XXX-ad_peak_8", "PMN_peak_5", "XXX-ad_peak_11", "XXX-ad_peak_12"
  )), .Names = c("pfc_chr", "pfc_chr_st", "peak_name"), row.names = c(NA, 
  -10L), class = c("tbl_df", "tbl", "data.frame"))
  
  
  df
  #&gt; # A tibble: 10 x 3
  #&gt;    pfc_chr pfc_chr_st      peak_name
  #&gt;      &lt;chr&gt;      &lt;int&gt;          &lt;chr&gt;
  #&gt;  1    chr1    3046442  XXX-ad_peak_1
  #&gt;  2    chr1    3119671 XXX-ad_peak_2a
  #&gt;  3    chr1    3164756     PMN_peak_2
  #&gt;  4    chr1    3167322     Ytb_peak_3
  #&gt;  5    chr1    3210838     PMN_peak_3
  #&gt;  6    chr1    3212196  XXX-ad_peak_6
  #&gt;  7    chr1    3249068  XXX-ad_peak_8
  #&gt;  8    chr1    3268246     PMN_peak_5
  #&gt;  9    chr1    3444892 XXX-ad_peak_11
  #&gt; 10    chr1    3451544 XXX-ad_peak_12
  </code></pre>
  
  <p>What I want to do is to extract the substring in <code>peak_name</code> as part of 
  dplyr pipe. The final desired result is:</p>
  
  <pre class="lang-r prettyprint-override"><code>   pfc_chr pfc_chr_st      peak_name        new_col
  1     chr1    3046442  XXX-ad_peak_1         XXX-ad
  2     chr1    3119671 XXX-ad_peak_2a         XXX-ad
  3     chr1    3164756     PMN_peak_2            PMN
  4     chr1    3167322     Ytb_peak_3            Ytb
  5     chr1    3210838     PMN_peak_3            PMN
  6     chr1    3212196  XXX-ad_peak_6         XXX-ad
  7     chr1    3249068  XXX-ad_peak_8         XXX-ad
  8     chr1    3268246     PMN_peak_5            PMN
  9     chr1    3444892 XXX-ad_peak_11         XXX-ad
  10    chr1    3451544 XXX-ad_peak_12         XXX-ad
  </code></pre>
  
  <p>I tried this but fail:</p>
  
  <pre class="lang-r prettyprint-override"><code>&gt; df %&gt;% mutate(new_col = stringr::str_match(peak_name, "^(.*?)\\_peak\\_*?"))
  Error in mutate_impl(.data, dots) : 
    Column `new_col` must be length 10 (the number of rows) or one, not 20
  </code></pre>
  
  <p>What's the right way to do it?</p>