R, using dplyr::mutate with ifelse containing a grepl() gives unexpected result
<p>What is wrong with this ifelse statement. </p>

<pre><code>df &lt;- data.frame(var1=c('ABC','CAB', 'AB'))
dplyr::mutate(df, var2=ifelse(grepl('^AB',var1), 'AB-starter', var1))
</code></pre>

<p>Gives </p>

<pre><code>  var1       var2
1  ABC AB-starter
2  CAB          3
3   AB AB-starter
</code></pre>

<p>I wanted (using mutate and a ifelse statement) the value of var1 in second element of var2 (that is when 'var1' does not start with "AB"):</p>

<pre><code>  var1       var2
1  ABC AB-starter
2  CAB        CAB
3   AB AB-starter
</code></pre>