w3resource

R Programming: Find the elements of a given vector that are not in another given vector

R Programming: Vector Exercise-19 with Solution

Write a R program to find the elements of a given vector that are not in another given vector.

Sample Solution :

R Programming Code :

a = c(0, 10, 10, 10, 20, 30, 40, 40, 40, 50, 60)
b = c(10, 10, 20, 30, 40, 40, 50)
print("Original vector-1:")
print(a)
print("Original vector-2:")
print(b)
print("Elements of a that are not in b:")
result = setdiff(a, b)
print(result)

Sample Output:

[1] "Original vector-1:"
 [1]  0 10 10 10 20 30 40 40 40 50 60
[1] "Original vector-2:"
[1] 10 10 20 30 40 40 50
[1] "Elements of a that are not in b:"
[1]  0 60                         

R Programming Code Editor:



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

Previous: Write a R program to list the distinct values in a vector from a given vector.
Next: Write a R program to reverse the order of given 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.