(This article was first published on

**Category: R | Everything Counts**, and kindly contributed to R-bloggers)
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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...