# %in%

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Category: R | Everything Counts**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I just stumbled across a really useful infix function in R: `%in%`

. It compares two vectors and returs a logical vector if there is a match or not for its left operand. Let us look at some examples:

> 1:10 %in% c(1,3,5,9) [1] TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE

\(x\) without \(y\):

> "%w/o%" <- function(x, y) x[!x %in% y] > (1:10) %w/o% c(3,7,12) [1] 1 2 4 5 6 8 9 10

In my particular use-case, I wanted to implement sampling without replacement in a loop, i.e. removing the sampled values of the previous iteration:

subsample <- sample(x=observations, size=sample_size) ... observations <- observations[!observations %in% subsample]

To

**leave a comment**for the author, please follow the link and comment on their blog:**Category: R | Everything Counts**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.