Possible Duplicate:
How to delete multiple values from a vector?
Is there any build-in function allowing us to remove a particular group of elements in a vector ?
example:
x<-c(2, 4, 6, 9, 10)
remove the vector c(4,9,10)
from x
If you need to remove multiple elements from the vector, the std::remove will copy each, not removed element only once to its final location, while the vector::erase approach would move all of the elements from the position to the end multiple times.
All the elements of the vector are removed using clear() function. erase() function, on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.
The unique() function in C++ helps remove all the consecutive duplicate elements from the array or vector. This function cannot resize the vector after removing the duplicates, so we will need to resize our vector once the duplicates are removed. This function is available in the <algorithm.
you can do this many ways here is one:
x[!x %in% c(4, 9, 10)]
Alternatively you could use ?is.element
x[!is.element(x, c(4,9,10))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With