How to find the common elements in multiple vectors in R?

R ProgrammingServer Side ProgrammingProgramming

To find the common elements in multiple vectors, we can follow the below steps −

  • First of all, create a number of vectors.
  • Use intersect function to find the common elements in all the vectors.

Create the vectors

Let’s create a number of vectors as shown below −

 Live Demo

x<-rpois(150,2)
y<-rpois(150,1)
z<-rpois(150,10)
a<-rpois(150,5)
b<-rpois(150,8)
c<-rpois(150,12)
x
y
z
a
b
c

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

Output

[1] 0 3 1 1 2 0 1 0 1 0 3 4 2 2 1 1 1 7 3 1 3 2 3 1 0 0 0 2 4 1 2 2 2 0 1 3 2
[38] 2 2 3 6 3 0 1 3 1 1 3 5 1 1 2 3 2 0 1 3 1 2 2 3 2 2 1 1 1 1 5 2 6 3 3 3 6
[75] 2 3 0 1 2 1 0 4 1 0 5 0 0 4 2 1 1 2 0 0 2 3 0 3 0 1 1 5 0 0 3 0 0 5 2 1 5
[112] 1 3 0 5 7 1 3 5 2 1 0 1 3 1 3 2 2 0 0 2 4 1 3 0 2 0 4 1 2 1 1 1 2 1 2 3 1
[149] 1 4

[1] 1 1 0 2 1 1 3 1 2 2 0 0 0 1 0 2 2 0 1 1 0 2 0 1 0 2 0 0 0 0 1 0 2 0 0 2 0
[38] 1 0 1 0 0 3 2 1 0 0 0 2 2 1 1 1 2 1 0 2 2 0 0 1 3 0 5 1 1 4 1 0 0 1 1 1 0
[75] 0 0 1 2 1 1 0 2 1 1 3 1 1 2 1 2 2 3 1 1 0 1 2 0 0 3 2 1 1 1 1 0 0 0 1 0 1
[112] 1 0 1 1 1 3 0 2 2 1 2 1 2 2 1 0 1 0 2 1 0 0 1 1 2 2 1 1 2 1 1 0 0 0 4 2 2
[149] 0 0

[1] 12 11 4 9 7 7 16 7 7 12 12 9 8 12 3 12 9 10 8 14 8 13 8 9 13
[26] 16 7 12 7 6 8 8 5 14 13 12 9 6 8 11 6 7 12 3 14 15 12 6 10 6
[51] 14 10 15 10 10 8 9 9 15 13 10 12 12 10 11 9 7 17 8 8 6 11 7 10 7
[76] 15 10 8 10 15 9 7 11 0 12 5 13 9 9 10 11 11 8 11 9 10 7 8 13 14
[101] 14 4 10 7 5 9 15 17 9 8 8 8 13 8 5 17 9 13 10 9 10 11 11 13 9
[126] 13 11 11 16 18 12 13 13 8 6 6 11 7 8 10 9 3 9 7 6 7 10 8 5 8

[1] 7 4 1 4 7 8 6 4 8 2 6 0 7 4 7 8 5 4 2 5 8 3 4 8 7
[26] 6 7 3 6 2 3 8 7 12 8 8 3 4 6 3 0 7 3 1 6 3 11 5 6 3
[51] 5 6 7 8 5 2 7 5 7 7 6 4 8 5 4 5 8 5 10 3 2 5 6 7 1
[76] 6 5 4 2 7 5 4 3 8 9 3 4 6 2 11 1 9 3 3 9 5 3 4 3 4
[101] 7 3 6 4 3 4 2 6 8 7 10 6 8 6 5 3 5 5 8 6 4 3 3 5 6
[126] 3 7 4 7 6 4 3 6 9 3 4 9 6 3 8 5 2 7 3 5 10 7 7 6 3

[1] 9 9 4 5 7 7 6 8 5 5 10 10 6 4 8 12 13 8 5 3 6 9 7 7 13
[26] 8 10 5 6 8 3 8 11 8 9 8 9 7 13 6 12 12 5 4 3 6 8 5 10 5
[51] 8 10 7 3 8 13 5 12 6 8 4 6 5 5 9 5 9 8 12 4 7 3 10 6 9
[76] 6 9 5 11 7 8 9 8 8 4 4 13 6 8 12 6 9 8 9 6 10 7 9 5 8
[101] 10 10 7 9 5 10 11 6 10 16 12 5 6 10 7 9 8 0 9 9 4 6 11 7 4
[126] 10 4 9 5 13 7 8 11 7 7 8 8 17 7 9 7 12 11 6 9 12 5 4 9 5

[1] 11 10 10 11 7 14 10 14 13 11 4 18 15 8 18 6 6 12 10 7 10 13 15 12 10
[26] 12 11 9 18 7 9 13 18 16 5 10 9 20 8 12 16 13 9 18 13 8 9 11 7 10
[51] 9 16 14 16 9 19 11 15 8 9 10 16 15 7 16 11 8 13 16 10 11 10 14 19 11
[76] 14 11 11 12 14 9 14 7 11 13 9 9 14 18 18 12 15 14 14 13 12 10 10 15 15
[101] 13 16 10 11 8 15 7 9 9 12 19 7 8 11 14 10 7 8 14 17 12 10 10 16 8
[126] 10 8 3 9 17 16 11 15 11 13 14 9 8 17 6 11 7 11 13 16 9 5 10 11 16

Find common elements

Using intersect function with Reduce function to find the common elements in all the above vectors −

 Live Demo

x<-rpois(150,2)
y<-rpois(150,1)
z<-rpois(150,10)
a<-rpois(150,5)
b<-rpois(150,8)
c<-rpois(150,12)
Reduce(intersect,list(x,y,z,a,b,c))

Output

[1] 3 4 5
raja
Published on 06-Aug-2021 13:24:51
Advertisements