# Determine the intersection beween multiple sets for R with multiple smiliar set possibility

Hello,

I am looking for a way to essentially use `vecsets::vintersect` with `Reduce` so I can run it for all lists. It is vitally important that I should be able to have multiple items returned as in the example as although the numbers are unique the amount matters.

You will see with `reduce(intersect)` it works in showing us the minimal intersect as where I would like the overall intersect between a_1, b_1, and c_1 (which should be 3,9,9). I essentially want to compare an arbitrary large size of collections and determine this "special" intersect. I am sure there might be a good existing solution hence asking about reduce. I essentially need the speed given I am going to be performing a lot of these sorts of comparisons.

Any help would be much appreciated!

``````library(vecsets)
#> Warning: package 'vecsets' was built under R version 4.0.5

a_1 <- c(1,3,3,5,7,9,9)
b_1 <- c(3,6,8,9,9)
c_1 <- c(2,3,3,4,5,7,9,3,9)

vecsets::vintersect(a_1,b_1, multiple = TRUE)
#>  3 9 9
vecsets::vintersect(a_1,c_1, multiple = TRUE)
#>  3 3 5 7 9 9

Reduce(intersect, list(a_1,b_1,c_1))
#>  3 9
``````

Created on 2022-02-22 by the reprex package (v2.0.0)

Turned out not to be that complicated and I was relatively close to a solution. Here it is. I am still not sure if it is fast enough for much much larger sets.

``````library(vecsets)
#> Warning: package 'vecsets' was built under R version 4.1.2

a_1 <- c(1,3,3,5,7,9,9)
b_1 <- c(3,6,8,9,9)
c_1 <- c(2,3,3,4,5,7,9,3,9)

vecsets::vintersect(a_1,b_1, multiple = TRUE)
#>  3 9 9

vecsets::vintersect(a_1,c_1, multiple = TRUE)
#>  3 3 5 7 9 9

Reduce(intersect, list(a_1,b_1,c_1))
#>  3 9

intersect_all <- function(a,b,...){
Reduce(vecsets::vintersect, list(a,b,...), accumulate = FALSE)
}

intersect_all(a_1,b_1,c_1)
#>  3 9 9
``````

Created on 2022-02-23 by the reprex package (v2.0.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.