Which functions should I use to find the union and intersection of two R vectors?

+2 votes

Best answer

You can use the **union()** function to find the union of two vectors. To find the intersection of two vectors, use the **intersect() **function.

Here is an example:

x <- c(10,20,30,40,50,60)

y <- c(10,200,300,40,500,60)intersect(x,y)

union(x,y)

The output of the above code will be:

> intersect(x,y)

[1] 10 40 60

> union(x,y)

[1] 10 20 30 40 50 60 200 300 500