Using the% in% operator in a data.table column

With the usual data.frameyou can do something like this:

head(mtcars[, 2]) %in% 6
[1]  TRUE  TRUE FALSE  TRUE FALSE  TRUE

How can I do the same with data.table? I tried something like this, didn't work:

as.data.table(mtcars)[, 2] %in% 4
[1] FALSE

Of course, there is always a way as.data.frame(myDT), but it does not look elegant.

+4
source share
1 answer

C data.table/tibble/data_frameetc. [,columnindex]for one column will return anyway data.table/tibble/data_frame. We need to either use $or [[to return vectorand %in%onvector

as.data.table(mtcars)[[2]] %in% 4
+3
source

Source: https://habr.com/ru/post/1691734/


All Articles