【发布时间】:2020-10-26 23:27:27
【问题描述】:
我想使用df2$x 转换df1$x 以获得df3。但是这样使用mutate肯定是错的。
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)
【问题讨论】: