w3resource

R Programming: Find common elements from multiple vectors

R Programming: Vector Exercise-15 with Solution

Write a R program to find common elements from multiple vectors.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 20, 20, 25, 29, 26)
y = c(10, 50, 30, 20, 20, 35, 19, 56)
z = c(10, 40, 30, 20, 20, 25, 49, 26)
print("Original Vectors:")
print("x: ")
print(x)
print("y: ")
print(y)
print("z: ")
print(z)
print("Common elements from above vectors:")
result = intersect(intersect(x,y),z)
print(result)

Sample Output:

[1] "Original Vectors:"
[1] "x: "
[1] 10 20 30 20 20 25 29 26
[1] "y: "
[1] 10 50 30 20 20 35 19 56
[1] "z: "
[1] 10 40 30 20 20 25 49 26
[1] "Common elements from above vectors:"
[1] 10 20 30                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to find nth highest value in a given vector.
Next: Write a R program to convert given dataframe column(s) to a vector.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.