title: mutate replace, how to specify as character
body: <p>I have this data in which I want to replace "20-Oct" with "10-20" in column "depth". Depth is a factor here. </p>
  
  <p><a href="https://i.stack.imgur.com/YmcVU.jpg" rel="nofollow noreferrer">BNPP</a></p>
  
  <p>This is what I tried:</p>
  
  <pre><code>BNPP &lt;- BNPP %&gt;%
    mutate(depth = replace(depth, depth == "20-Oct", "10-20"))
  </code></pre>
  
  <p>And I got this error message: </p>
  
  <pre><code>Warning message:
  In `[&lt;-.factor`(`*tmp*`, list, value = "10-20") :
    invalid factor level, NA generated
  </code></pre>
  
  <p>So I tried to fix it by doing this:</p>
  
  <pre><code>BNPP &lt;- BNPP %&gt;%
    BNPP$depth &lt;- as.character(BNPP$depth) %&gt;%
    mutate(depth = replace(depth, depth == "20-Oct", "10-20"))
  </code></pre>
  
  <p>And I got a different error message:</p>
  
  <pre><code> Error in UseMethod("mutate_") : 
        no applicable method for 'mutate_' applied to an object of class "character"
  </code></pre>
  
  <p>I tried putting the as.character outside the code chunk and it worked, but I have no idea why:</p>
  
  <pre><code>BNPP$depth &lt;- as.character(BNPP$depth)
  BNPP &lt;- BNPP %&gt;%
    mutate(depth = replace(depth, depth == "20-Oct", "10-20"))
  </code></pre>
  
  <p>I would appreciate an explanation for why the first one doesn't work but the last one works.</p>