Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R data.table intersection of all groups

Tags:

r

data.table

I want to have the intersection of all groups of a data table. So for the given data:

data.table(a=c(1,2,3, 2, 3,2), myGroup=c("x","x","x",  "y",  "z","z"))

I want to have the result:

2

I know that

Reduce(intersect, list(c(1,2,3), c(2), c(3,2)))

will give me the desired result but I didn't figure out how to produce a list of groups of a data.table query.

like image 473
Willi Müller Avatar asked Dec 16 '14 20:12

Willi Müller


1 Answers

I would try using Reduce in the following way (assuming dt is your data)

Reduce(intersect, dt[, .(list(unique(a))), myGroup]$V1)
## [1] 2
like image 184
David Arenburg Avatar answered Sep 28 '22 08:09

David Arenburg