title: Mutate repeats first row value
body: <p>I have a dataset with taxonomy assignment and I want to extract the genus in a new column.</p>
  
  <pre><code>library(tidyverse)
  library(magrittr)
  library(stringr)
  
  
  df &lt;- structure(list(C043 = c(18361L, 59646L, 27575L, 163L, 863L, 3319L, 
                                0L, 6L), C057 = c(20020L, 97610L, 13427L, 1L, 161L, 237L, 2L, 
                                                  105L), taxonomy = structure(c(3L, 2L, 1L, 6L, 4L, 4L, 5L, 2L), .Label = c("k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Enterobacter;NA", 
                                                                                                                            "k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Enterobacter;s__cloacae", 
                                                                                                                            "k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Escherichia;s__coli", 
                                                                                                                            "k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Klebsiella;s__", 
                                                                                                                            "k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Pseudomonadales;f__Pseudomonadaceae;g__Pseudomonas;s__", 
                                                                                                                            "k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Pseudomonadales;f__Pseudomonadaceae;g__Pseudomonas;s__stutzeri"
                                                  ), class = "factor")), .Names = c("C043", "C057", "taxonomy"), row.names = c(1L, 
                                                                                                                               2L, 3L, 4L, 5L, 6L, 8L, 10L), class = "data.frame")
  </code></pre>
  
  <p>So this is my function (it works)</p>
  
  <pre><code>extract_genus &lt;- function(str){
    genus &lt;- str_split(str, pattern = ";")[[1]][6] 
    genus %&lt;&gt;% str_sub(start = 4) #%&gt;% as.character
    return(genus)
  }
  </code></pre>
  
  <p>But when I applied it in <code>mutate</code> (with or without <code>as.character</code>), it repeats first row value in the new column.  </p>
  
  <pre><code>df %&gt;% mutate(genus = extract_genus(taxonomy))
  
     C043  C057                                                                                                                   taxonomy       genus
  1 18361 20020     k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Escherichia;s__coli Escherichia
  2 59646 97610 k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Enterobacter;s__cloacae Escherichia
  3 27575 13427         k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Enterobacter;NA Escherichia
  4   163     1     k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Pseudomonadales;f__Pseudomonadaceae;g__Pseudomonas;s__stutzeri Escherichia
  5   863   161          k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Klebsiella;s__ Escherichia
  </code></pre>
  
  <p>When I use <code>sapply</code> (but I don't want to, I want a solution with <code>dplyr</code> pipeline), it works.</p>
  
  <pre><code>df_group_gen$genus &lt;- sapply(df_group_gen$taxonomy, extract_genus)
  
     C043  C057                                                                                                                   taxonomy        genus
  1 18361 20020     k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Escherichia;s__coli  Escherichia
  2 59646 97610 k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Enterobacter;s__cloacae Enterobacter
  3 27575 13427         k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Enterobacter;NA Enterobacter
  4   163     1     k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Pseudomonadales;f__Pseudomonadaceae;g__Pseudomonas;s__stutzeri  Pseudomonas
  5   863   161          k__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacteriales;f__Enterobacteriaceae;g__Klebsiella;s__   Klebsiella
  </code></pre>
  
  <p>Why <code>mutate</code> doesn't compute as we can expect? I find this <a href="https://stackoverflow.com/questions/30423116/why-mutate-applied-only-to-first-row-and-repeats-its-result-to-the-rest">question</a> but no answer is provided, only a <em>had hoc</em> code.</p>
  
  <p>Thank you :)</p>