title: Why are my factor levels turning to numbers with dplyr::mutate()?
body: <p>I'm trying to recode some factor levels using dplyr::mutate(), so that all "MOT" and "CHI" factor levels remain as they are, and all others turn to "OTH". </p>
  
  <p>My data looks like this:</p>
  
  <pre><code>subj | speaker | word
  
  1        MOT     apple
  1        CHI     baby
  1        SI1     baby
  2        CHI     dog
  2        CHI     cat
  2        FAT     cat
  </code></pre>
  
  <p>And I want it to looks like this:</p>
  
  <pre><code>subj | speaker | word
  
  1        MOT     apple
  1        CHI     baby
  1        OTH     baby
  2        CHI     dog
  2        CHI     cat
  2        OTH     cat
  </code></pre>
  
  <p>My code looks like this:</p>
  
  <pre><code>new.df &lt;- data %&gt;%
    dplyr::select(subj, speaker) %&gt;%
          mutate(speaker = factor(speaker),
           speaker = ifelse(speaker %in% c("CHI", "MOT"), speaker, "OTH")) 
  </code></pre>
  
  <p>However, when I run this script some of the levels are converted to "OTH" but all of the others change to numbers. This happens regardless of whether I include the factor(speaker) command:</p>
  
  <pre><code>subj | speaker | word
  
  1        175     apple
  1        86      baby
  1        OTH     baby
  2        86      dog
  2        86      cat
  2        OTH     cat
  </code></pre>
  
  <p>It is changing the factors to "OTH" ok but the TRUE element of my ifelse code appears to be the problem.</p>
  
  <p>Many thanks!</p>