Suppose I have the following data.table
dx <- data.table(x=c(1,2,3),tet=list(c(1,2,3),c(4,5),c(3)))
x tet
1: 1 1,2,3
2: 2 4,5
3: 3 3
I wanna create a variable test
that checks wether each elements of x is in its corresponding set in tet
.
I woud like to get
x tet test
1: 1 1,2,3 TRUE
2: 2 4,5 FALSE
3: 3 3 TRUE
I have tried
dx[, test:= x %in% tet]
x tet test
1: 1 1,2,3 FALSE
2: 2 4,5 FALSE
3: 3 3 TRUE
But of course the first row fails because it is comparing 1
against c(1,2,3)
and the second row gives me a a false FALSE
.
I have also tried using lapply and unlisting but to no avail.
Any ideas?