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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With