I want to transform df1$x using df2$x to obtain df3. But I am certainly wrong when using mutate this way.
library(tidyverse)
df1 <- tibble(year = c(2019, 2019, 2020, 2020),
x = c("0123", "0222", "0144", "0124"))
df2 <- tibble(x = c("22", "24"))
# I want to obtain
df3 <- tibble(year = c(2019, 2019, 2020, 2020),
x = c("0123", "0222", "0144", NA))
# but this mutate does not work
df1 %>%
mutate(x = if_else(str_sub(x,3,4) %in% df2$x & year == 2020, NA, x))
#> Error: Problem with `mutate()` input `x`.
#> x `false` must be a logical vector, not a character vector.
#> i Input `x` is `if_else(str_sub(x, 3, 4) %in% df2$x & year == 2020, NA, x)`.
Created on 2020-10-26 by the reprex package (v0.3.0)