R% in% operator behavior for data table factors?

I cannot get the% in% operator to behave for the data table factor columns. I know I'm probably missing some secret syntax for data tables, but I can't find it ... I've searched everything.

Here's a tiny example to illustrate my pain. Surely a simple answer would be to use data frames, but I have a large dataset that benefits from some of the features of data tables.

> a <- data.table(c1=factor(c(1,2,3)))
> a
   c1
1:  1
2:  2
3:  3

> '2' %in% a[,1,with=F]
[1] FALSE

> 2 %in% a[,1,with=F]
[1] FALSE

      

and it works as I expect for data frames ...

> b <- data.frame(c1=factor(c(1,2,3)))
> '2' %in% b[,1]
[1] TRUE

      

Any help is appreciated ....

+3


source to share


1 answer


a[,1,with=F]

is a data.table, not a vector like b[,1]

. This is documented.

The data table is a list and help("%in%")

says that "lists are converted to character vectors". So my guess is this is happening (but it's hidden in the C source code match

):

as.character(a[,1,with=F])
#[1] "1:3"

      



You can efficiently select data.table columns with [[

:

'2' %in% a[[1]]
#[1] TRUE

      

+6


source







All Articles