# How to find the union of two vectors in R?

The union of vectors return all the unique values in both the vectors. For example, if we have a vector x that contains 1, 2, 3, 4, 2, 3, 4, 1, 1, 4 and another vector that contains 2, 1, 2, 4, 5, 7, 5, 1, 2, 3, 7, 6, 5, 7, 4, 2, 4, 1, 5, 8, 1, 3 then the union of these two vectors will be 1, 2, 3, 4, 5, 6, 7, 8. In R, we can do this by using union function.

## Example

Live Demo

> x1<-rpois(10,2)
> x1

## Output

[1] 2 2 0 1 1 1 4 1 2 2

## Example

Live Demo

> y1<-rpois(10,5)
> y1

## Output

[1] 3 5 6 6 10 3 5 9 3 8

## Example

> union(x1,y1)

## Output

[1] 2 0 1 4 3 5 6 10 9 8

## Example

Live Demo

> x2<-rpois(100,5)
> x2

## Output

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

## Example

Live Demo

> y2<-rpois(100,10)
> y2

## Output

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

## Example

> union(x2,y2)

## Output

[1] 10 5 4 7 9 3 6 8 2 1 11 17 13 15 18 12 16 14 19

## Example

Live Demo

> x3<-sample(0:9,200,replace=TRUE)
> x3

## Output

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

## Example

Live Demo

> y3<-sample(1:50,200,replace=TRUE)
> y3

## Output

[1] 2 31 7 14 40 15 14 21 21 16 5 26 21 44 11 18 40 25 5 42 38 45 28 42 4
[26] 19 35 16 32 8 32 33 34 16 44 23 19 11 5 16 12 13 35 7 17 17 16 35 36 32
[51] 17 31 33 13 12 20 48 43 21 36 23 35 21 22 23 1 48 45 27 49 48 42 41 3 37
[76] 2 12 34 16 41 32 39 13 13 37 29 50 22 45 28 12 19 10 31 49 37 20 23 27 33
[101] 20 10 27 33 7 5 35 15 10 7 34 12 1 46 12 2 2 36 13 29 45 4 36 1 27
[126] 42 8 34 24 11 45 39 39 33 12 41 41 45 6 2 16 40 39 14 32 7 29 12 13 16
[151] 33 3 30 2 35 6 33 30 43 25 34 42 38 4 24 40 15 45 18 41 10 9 24 4 42
[176] 11 32 14 8 49 47 6 38 45 20 30 48 26 17 49 34 24 12 31 24 26 3 13 9 6

## Example

> union(x3,y3)

## Output

[1] 1 7 5 9 2 6 8 0 4 3 31 14 40 15 21 16 26 44 11 18 25 42 38 45 28
[26] 19 35 32 33 34 23 12 13 17 36 20 48 43 22 27 49 41 37 39 29 50 10 46 24 30
[51] 47

## Example

Live Demo

> x4<-round(runif(200,2,4))
> x4

## Output

[1] 3 3 4 4 4 2 2 3 2 4 4 3 4 2 3 3 3 4 4 3 3 3 3 3 2 4 4 3 3 3 4 3 4 3 4 3 3
[38] 2 3 3 3 3 2 4 2 4 2 3 2 2 3 3 4 3 3 4 3 3 2 2 3 3 3 4 2 3 4 3 3 3 4 3 3 4
[75] 4 4 3 3 3 3 2 3 3 3 3 3 4 2 3 3 2 2 4 3 2 3 3 3 3 2 2 3 3 3 2 2 3 4 3 2 3
[112] 3 2 4 3 3 3 4 3 3 3 3 3 3 4 4 3 3 3 3 4 4 3 4 2 4 4 3 2 2 3 2 3 2 2 3 4 3
[149] 3 2 4 3 2 3 3 3 3 3 4 3 2 2 3 2 4 3 2 3 3 2 4 4 4 3 3 3 2 3 3 3 3 3 3 2 3
[186] 3 3 3 4 4 4 2 4 2 4 4 4 2 4 3

## Example

Live Demo

> y4<-round(runif(200,2,10))
> y4

## Output

[1] 9 6 3 5 6 2 8 3 9 5 6 4 4 7 6 5 8 3 7 4 5 7 6 7 4
[26] 9 3 10 9 3 6 5 9 3 5 6 3 7 3 2 7 8 7 3 8 8 10 6 6 10
[51] 5 7 3 7 6 9 6 7 9 5 3 8 5 8 5 10 3 9 5 10 7 6 5 9 4
[76] 8 10 6 6 8 6 5 10 7 3 9 4 5 10 5 10 3 2 5 8 3 3 5 9 9
[101] 6 5 8 9 9 6 5 3 6 8 3 4 3 9 10 9 7 2 9 10 2 10 10 8 4
[126] 9 9 5 7 2 4 8 7 8 8 3 9 3 7 5 8 3 8 5 9 4 8 8 10 2
[151] 4 3 10 5 4 4 6 6 3 7 6 6 6 9 3 2 6 2 3 2 4 2 7 5 3
[176] 4 3 9 4 2 7 9 7 10 8 8 2 6 3 4 9 6 10 6 2 3 9 5 9 7

## Example

Live Demo

> union(x3,y4)

## Output

[1] 1 7 5 9 2 6 8 0 4 3 10

## Example

Live Demo

> x5<-round(runif(200,0,20))
> x5

## Output

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

## Example

Live Demo

> y5<-round(runif(200,18,30))
> y5

## Output

[1] 23 18 24 29 20 22 22 23 28 26 23 28 27 22 19 25 23 27 28 27 23 25 29 19 25
[26] 25 27 27 24 30 24 20 29 27 27 27 19 18 21 23 19 19 27 27 22 25 27 19 28 23
[51] 27 25 24 24 19 21 19 24 30 21 26 19 20 24 30 23 19 25 19 29 23 25 21 20 25
[76] 29 22 29 19 19 24 24 29 23 27 22 25 20 26 23 24 30 28 19 26 22 24 19 19 24
[101] 28 24 24 26 29 26 29 22 22 21 24 30 27 25 28 21 22 21 28 23 25 23 20 22 23
[126] 25 21 20 24 26 18 29 25 18 19 21 18 18 28 30 22 30 26 28 19 27 20 26 21 20
[151] 18 25 21 20 26 30 25 22 22 22 22 25 23 19 22 25 27 25 19 19 29 19 24 30 18
[176] 26 20 19 24 20 25 20 27 28 28 20 26 20 28 21 29 25 25 25 20 24 28 30 23 20

## Example

Live Demo

> union(x5,y5)

## Output

[1] 1 20 11 4 19 5 8 0 14 3 18 2 9 12 15 7 6 10 13 17 16 23 24 29 22
[26] 28 26 27 25 30 21

Updated on: 07-Nov-2020

2K+ Views