title: How to use dplyr to modify a column of factors?
body: <p>I have the following dataset:</p>
  
  <pre><code>library(magrittr)
  x &lt;- structure(
    list(col1 = structure(
      c(1L, 1L, 2L, 1L, 3L),
      .Label = c("C",
                 "Q", "S"),
      class = "factor"
    )),
    .Names = "col1",
    row.names = c(NA, -5L),
    class = c("tbl_df", "data.frame")
  )
  </code></pre>
  
  <p>I would like to replace all rows with value 'S' in col1 with 'C'.</p>
  
  <p>This works as expected:</p>
  
  <pre><code>x[x$col1 == 'S',] &lt;- 'C'
  </code></pre>
  
  <p>I tried to do the replacement with dplyr using the following code:</p>
  
  <pre><code>x %&gt;%
    dplyr::mutate(col1 = ifelse(col1 == 'S', 'C', col1))
  </code></pre>
  
  <p>But it instead gave a column of integers where each integer represents the corresponding level for how the factor variable i.e. col1 was coded:</p>
  
  <pre><code>Source: local data frame [5 x 1]
  
     col1
    (int)
  1     1
  2     1
  3     2
  4     1
  5     1
  </code></pre>
  
  <p>Why did dplyr do this and what is the proper way to do the replacement using dplyr?</p>
  
  <p>Output of <code>sessionInfo()</code>:</p>
  
  <pre><code>R version 3.3.2 (2016-10-31)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows &gt;= 8 x64 (build 9200)
  
  locale:
  [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
  [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
  [5] LC_TIME=English_United States.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
  [1] magrittr_1.5
  
  loaded via a namespace (and not attached):
  [1] lazyeval_0.2.0 R6_2.1.2       assertthat_0.1 parallel_3.3.2 DBI_0.3.1      tools_3.3.2   
  [7] dplyr_0.4.3    Rcpp_0.12.7
  </code></pre>