title: How to mutate some values of a dataframe based on values from another dataframe column with R
body: <p>I want to transform <code>df1$x</code> using <code>df2$x</code> to obtain <code>df3</code>. But I am certainly wrong when using <code>mutate</code> this way.</p>
  <pre><code>library(tidyverse)
  df1 &lt;- tibble(year = c(2019, 2019, 2020, 2020),
                x = c(&quot;0123&quot;, &quot;0222&quot;, &quot;0144&quot;, &quot;0124&quot;))
  df2 &lt;- tibble(x = c(&quot;22&quot;, &quot;24&quot;))
  
  # I want to obtain
  df3 &lt;- tibble(year = c(2019, 2019, 2020, 2020),
                x = c(&quot;0123&quot;, &quot;0222&quot;, &quot;0144&quot;, NA))
  
  # but this mutate does not work
  df1 %&gt;%
    mutate(x = if_else(str_sub(x,3,4) %in% df2$x &amp; year == 2020, NA, x))
  #&gt; Error: Problem with `mutate()` input `x`.
  #&gt; x `false` must be a logical vector, not a character vector.
  #&gt; i Input `x` is `if_else(str_sub(x, 3, 4) %in% df2$x &amp; year == 2020, NA, x)`.
  Created on 2020-10-26 by the reprex package (v0.3.0)
  </code></pre>