- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the union of three vectors in R?
The union function in base R helps us to find the union of two vectors but if we have three vectors then the union cannot be directly created. For this purpose, we need to use union function twice. For example, if we have three vectors defined as x, y, and z then the union of these vectors can be found by using the command union(x,union(y,z)).
Example1
> x1<-1:5 > y1<-6:10 > z1<-11:15 > union(x1,union(y1,z1))
Output
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example2
> x2<-rpois(100,10) > x2
Output
[1] 13 6 16 11 9 11 3 15 12 12 13 9 13 11 17 12 17 6 8 10 16 13 6 9 16 [26] 5 8 14 7 14 7 13 9 9 14 13 12 8 9 10 10 9 8 10 10 9 12 9 7 11 [51] 10 12 15 11 10 13 9 7 12 9 13 13 14 7 11 12 12 11 11 11 10 6 9 11 4 [76] 9 7 10 6 11 18 12 9 9 12 11 10 4 8 9 7 4 8 10 7 13 10 11 11 10
Example
> y2<-rpois(100,8) > y2
Output
[1] 13 5 13 12 4 6 6 13 6 9 9 6 12 3 7 7 8 9 7 4 11 9 6 13 5 [26] 4 11 9 13 11 9 15 10 7 6 7 16 9 9 7 5 7 7 11 6 4 11 7 9 2 [51] 6 8 6 8 6 9 13 7 11 7 11 7 9 7 9 8 5 7 6 16 1 8 7 5 8 [76] 7 4 14 8 7 6 4 7 9 13 11 9 11 6 6 9 7 8 8 9 12 2 8 6 11
Example
> z2<-rpois(100,5) > z2
Output
[1] 5 7 3 4 3 4 10 3 2 4 4 5 7 7 4 4 4 5 7 3 2 7 1 5 5 [26] 3 5 6 5 4 4 1 7 6 5 7 6 6 4 4 7 5 4 1 3 5 1 4 5 5 [51] 6 1 4 1 7 1 4 4 7 5 6 5 4 4 5 5 2 4 1 6 0 2 3 7 6 [76] 6 4 4 8 7 3 11 5 3 2 2 4 5 4 5 5 5 1 6 4 2 8 6 4 5
Example
> union(x2,union(y2,z2))
Output
[1] 13 6 16 11 9 3 15 12 17 8 10 5 14 7 4 18 2 1 0
Example
> x3<-rpois(100,1) > x3
Output
[1] 2 2 0 0 2 0 2 1 0 1 1 2 1 0 1 2 0 0 0 2 1 3 1 2 1 0 1 0 0 1 2 0 2 0 1 1 2 [38] 3 0 2 1 0 0 4 2 2 0 2 2 1 2 1 1 0 0 1 1 3 2 0 4 2 0 2 4 1 0 0 0 0 0 0 0 1 [75] 0 2 1 1 2 3 2 0 1 0 2 1 0 0 1 2 0 1 0 0 0 1 2 1 0 1
Example
> y3<-rpois(100,2) > y3
Output
[1] 5 2 0 2 0 2 1 3 1 2 0 5 1 0 0 2 2 0 1 2 2 2 3 0 2 3 1 0 3 4 3 3 1 0 2 1 3 [38] 1 3 3 5 0 1 2 1 4 5 4 2 2 2 3 3 1 4 5 2 0 2 1 3 3 3 3 1 1 2 2 1 2 3 2 3 3 [75] 2 0 2 4 3 1 3 2 2 2 2 4 1 1 1 0 3 3 4 4 1 1 2 3 1 1
Example
> z3<-rpois(100,5) > z3
Output
[1] 4 8 4 6 7 6 4 3 3 5 3 7 4 7 7 1 4 6 9 3 7 8 4 4 2 [26] 6 9 6 4 6 2 5 4 10 6 4 6 5 4 4 3 5 1 4 3 2 8 6 4 5 [51] 11 8 5 7 2 3 4 4 3 0 5 6 8 4 6 8 4 6 2 8 1 5 7 4 5 [76] 8 6 5 3 7 3 3 4 8 3 7 7 4 3 7 3 8 3 4 7 4 4 6 9 6
Example
> union(x3,union(y3,z3))
Output
[1] 2 0 1 3 4 5 8 6 7 9 10 11
Example4
> x4<-round(runif(120,1,2),0) > x4
Output
[1] 2 1 2 2 1 1 1 1 2 1 1 1 2 2 2 2 1 2 1 2 1 1 2 1 2 2 2 1 1 1 1 2 2 2 1 1 2 [38] 2 1 1 2 1 1 2 1 1 1 1 1 2 2 1 2 1 1 2 2 2 2 2 1 2 1 1 1 2 2 2 2 2 1 2 2 2 [75] 2 2 1 2 1 2 2 1 2 2 1 1 2 1 2 1 1 1 1 1 1 2 2 1 1 1 2 2 1 1 2 2 1 2 2 2 1 [112] 1 2 2 1 1 1 2 1 1
Example
> y4<-round(runif(120,1,5),0) > y4
Output
[1] 2 3 5 4 3 5 5 5 4 3 3 4 4 1 4 3 2 4 2 2 4 5 4 5 4 4 3 5 5 2 2 4 2 1 3 2 1 [38] 5 2 4 5 4 3 2 3 3 4 2 1 4 3 2 2 3 2 4 3 4 3 4 4 1 4 2 4 2 3 1 4 2 4 1 5 3 [75] 4 2 5 5 4 4 2 4 4 4 2 5 5 2 2 3 5 3 5 2 4 2 2 4 2 1 3 2 3 3 3 3 2 2 1 4 3 [112] 5 4 1 4 2 3 4 4 4
Example
> z4<-round(runif(120,2,10),0) > z4
Output
[1] 8 8 4 5 9 6 6 8 4 6 3 6 5 9 3 9 3 6 10 7 9 9 8 2 4 [26] 7 6 10 6 6 3 4 8 4 8 5 6 8 5 8 10 9 7 10 3 5 7 8 10 4 [51] 9 4 8 3 9 8 9 3 4 10 6 10 9 7 6 7 7 9 2 10 10 6 4 9 6 [76] 8 7 5 9 6 8 8 6 5 6 9 7 2 7 4 4 3 8 8 6 6 4 9 2 10 [101] 5 9 5 7 10 7 3 9 4 6 6 8 5 4 7 5 5 9 5 2
Example
> union(x4,union(y4,z4)) [1] 2 1 3 5 4 8 9 6 10 7
Example5
> x5<-round(rnorm(120,1,0.5),0) > x5
Output
[1] 1 1 1 2 1 1 1 1 0 2 1 2 0 1 1 1 1 2 1 1 1 1 1 1 1 [26] 0 1 2 1 2 0 1 1 1 1 1 0 0 2 1 1 1 2 1 2 1 1 1 1 1 [51] -1 1 2 0 1 1 1 0 1 1 2 1 1 1 1 1 0 1 1 2 1 0 0 0 2 [76] 1 1 1 1 0 1 1 1 1 2 1 1 1 0 1 1 0 1 0 0 1 1 1 1 1 [101] 1 0 1 0 1 2 0 2 1 1 1 1 1 1 1 1 1 1 1 1
Example
> y5<-round(rnorm(120,5,0.25),0) > y5
Output
[1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 5 5 5 5 5 6 5 5 5 5 5 5 6 5 5 5 5 5 5 5 5 [38] 5 5 5 5 5 6 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 [75] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 [112] 5 5 5 5 5 5 5 5 5
Example
> z5<-round(rnorm(120,5,3),0) > z5
Output
[1] 9 4 5 6 6 8 5 8 8 5 8 0 5 2 6 5 0 4 6 7 0 7 10 8 8 [26] 6 10 2 4 1 6 9 4 2 6 7 5 2 10 5 5 -2 6 0 5 5 8 7 1 4 [51] 5 9 0 7 4 10 4 4 2 6 2 3 6 7 8 8 8 5 1 7 4 5 8 6 2 [76] 7 0 1 7 9 1 6 5 8 2 10 8 3 3 1 6 4 5 6 11 8 8 3 10 4 [101] 7 1 5 4 2 2 3 6 8 8 7 3 1 8 7 4 4 4 3 5
Example
> union(x5,union(y5,z5))
Output
[1] 1 2 0 -1 5 6 9 4 8 7 10 -2 3 11
- Related Articles
- How to find the union of two vectors in R?
- How to find the covariance between two vectors in R?
- How to find the common elements in multiple vectors in R?
- How to find the unique elements in multiple vectors in R?
- How to find pairwise maximum among multiple vectors in R?
- How to combine array of vectors in R?
- How to find the sum of division in R if zero exists in the vectors?
- How to find the number of common words between two string vectors in R?
- How to find the cross product of two vectors in R by adding the elements?
- How to find different elements between two string vectors in R?
- How to create combination of multiple vectors in R?
- How to find the mean of three-dimensional array in R?
- How to print string vectors vertically in R?
- How to Find the Union of Two Arrays in Java?
- How to match two string vectors if the strings case is different in both the vectors in R?

Advertisements