R data.table intersection of all groups

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

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

I want to get 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 did not understand how to create a list of query groups for data.table.

+6
source share
2 answers

I would try using Reduce as follows (assuming dt is your data)

 Reduce(intersect, dt[, .(list(unique(a))), myGroup]$V1) ## [1] 2 
+5
source

Here is one approach.

 nGroups <- length(unique(dt[,myGroup])) dt[, if(length(unique(myGroup))==nGroups) .BY else NULL, by="a"][[1]] # [1] 2 

And with some explanatory comments.

 ## Mark down the number of groups in your data set nGroups <- length(unique(dt[,myGroup])) ## Then, use `by="a"` to examine in turn subsets formed by each value of "a". ## For subsets having the full complement of groups ## (ie those for which `length(unique(myGroup))==nGroups)`, ## return the value of "a" (stored in .BY). ## For the other subsets, return NULL. dt[, if(length(unique(myGroup))==nGroups) .BY else NULL, by="a"][[1]] # [1] 2 

If this code and comments are not understood on their own, a quick look at the following may help. Basically, the approach described above simply searches and reports the value of a for those groups that return x,y,z in column V1 below.

 dt[,list(list(unique(myGroup))), by="a"] # a V1 # 1: 1 x # 2: 2 x,y,z # 3: 3 x,z 
+2
source

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


All Articles