Two options using data.table
First one uses %chin%
operator :
library(data.table)
x = data.table(v = LETTERS[1:3])
y = data.table(v = c("A","D","E","F"))
x[, found:= v %chin% y$v]
x
#> v found
#> 1: A TRUE
#> 2: B FALSE
#> 3: C FALSE
The second one is built on merging behaviour:
library(data.table)
x = data.table(v = LETTERS[1:3])
y = data.table(v = c("A","D","E","F"))
y[, found := TRUE]
x[, found:= y[.SD, .(ifelse(is.na(found), FALSE, TRUE)), on = .(v)]]
x
#> v found
#> 1: A TRUE
#> 2: B FALSE
#> 3: C FALSE
EDIT: Based on @frank comment, you could simplify with no ifelse
- it is the same
x[, found:= y[.SD, !is.na(found), on = .(v)]]
x
#> v found
#> 1: A TRUE
#> 2: B FALSE
#> 3: C FALSE
For understanding what happens, here is the inside code I built on:
x[, found := NULL]
y[x, on = .(v)]
#> v found
#> 1: A TRUE
#> 2: B NA
#> 3: C NA