Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a particular group of elements in a vector? [duplicate]

Tags:

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

like image 475
user1415530 Avatar asked Jul 26 '12 15:07

user1415530


People also ask

How do I remove multiple elements from a vector file?

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.

How do I remove a specific element from a vector?

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.

Can vector have duplicates in C++?

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.


2 Answers

you can do this many ways here is one:

x[!x %in% c(4, 9, 10)]
like image 179
Justin Avatar answered Oct 23 '22 01:10

Justin


Alternatively you could use ?is.element

x[!is.element(x, c(4,9,10))]
like image 43
johannes Avatar answered Oct 23 '22 00:10

johannes