Reputation: 85
In R, vector myvector contains several vectors.How coule I compare all vectors within myvector with each other.I I want to have the intersection between each two vectors within myvector.
a <- c(1,2,3,4)
b <- c(1,3,5,7,8,9)
d <- c(1,2,5,7,9)
e <- c(1,3,4,8,0,10)
f <- c(2,3)
myvector <- c(a,b,d,e,f)
In short,how to simplify the modes as following.
intersect1 <- intersect(a,b)
intersect2 <- intersect(a,d)
intersect3 <- intersect(a,e)
intersect4 <- intersect(a,f)
intersect5 <- intersect(b,d)
#......
interscet_abd <- intersect(1,d)
interscet_abe <- intersect(1,e)
#......
intersect_abdef <- intersect(abde,f)
Upvotes: 4
Views: 1472
Reputation: 39647
You can use combn
to get all combinations. Doing this in a loop for 2:5
number of elements to choose and using intersect
in Reduce
it could be done like:
L <- list(a,b,d,e,f)
lapply(2:5, function(i) {
apply(combn(1:5, i), 2, function(j) Reduce(intersect, L[j]))
})
or by adding names:
L <- list(a=a,b=b,d=d,e=e,f=f)
unlist(lapply(2:5, function(i) {
k <- combn(1:5, i)
x <- apply(k, 2, function(j) Reduce(intersect, L[j]), simplify = FALSE)
setNames(x, apply(k, 2, function(j) paste(names(L)[j], collapse = "")))
}), FALSE)
#$ab
#[1] 1 3
#
#$ad
#[1] 1 2
#
#$ae
#[1] 1 3 4
#...
#$bdef
#numeric(0)
#
#$abdef
#numeric(0)
Upvotes: 3
Reputation: 145765
Put your vectors in a list
and then you can use outer
with a Vectorize
d intersect
:
l <- list(a,b,d,e,f)
names(l) = c("a", "b", "d", "e", "f")
result = outer(l, l, Vectorize(intersect))
result
# a b d e f
# a numeric,4 numeric,2 numeric,2 numeric,3 numeric,2
# b numeric,2 numeric,6 numeric,4 numeric,3 3
# d numeric,2 numeric,4 numeric,5 1 2
# e numeric,3 numeric,3 1 numeric,6 3
# f numeric,2 3 2 3 numeric,2
result["a", "d"]
# [[1]]
# [1] 1 2
Inspired by this answer.
Depending on your use case, you may want to improve this idea -- intersect
is commutative, and the interesection of a vector with itself isn't interesting, so by doing every ordered pair of combinations, this does a lot of extra calculations. If you have a lot of vectors, using combn
to generate all unique unordered pairs would be more efficient, but this works quickly enough for a small number of inputs.
Upvotes: 3