<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-write-and-document-special-functions-in-r/"></div></div><div><a data-url="https://www.r-bloggers.com/how-to-write-and-document-special-functions-in-r/" data-counturl="https://www.r-bloggers.com/how-to-write-and-document-special-functions-in-r/" data-count="horizontal" data-text="How to write and document %special% functions in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://peter.solymos.org/code/2016/11/26/how-to-write-and-document-special-functions-in-r.html"> Peter Solymos - R related posts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I spend a considerable time of my working hours with data processing where I often use the <code>%in%</code> R function as <code>x %in% y</code>. Whenever I need the negation of that, I used to write <code>!(x %in% y)</code>. Not much of a hassle, but still, wouldn’t it be nicer to have <code>x %notin% y</code> instead? So I decided to code it for my <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=mefa4"><strong>mefa4</strong></a> package that I maintain primarily to make my data munging time shorter and more efficient. Coding a <code>%special%</code> function was no big deal. But I had to do quite a bit of research and trial-error until I figured out the proper documentation. So here it goes.</p>
<h2>The function</h2>
<p>The function name needs quotes and exactly two arguments, one for the left and one for the right hand side of the operator in the middle:</p>
<div>
<pre><code>&quot;%notin%&quot; &lt;- function(x, table) !(match(x, table, nomatch = 0) > 0)
</code></pre>
</div>
<p>Let us see what it does:</p>
<div>
<pre><code>1:4 %in% 3:5
## [1] FALSE FALSE  TRUE  TRUE
1:4 %notin% 3:5
## [1]  TRUE  TRUE FALSE FALSE
</code></pre>
</div>
<h2>The NAMESPACE entry</h2>
<p>We need to export the function, so just add the following entry to the <code>NAMESPACE</code> file:</p>
<div>
<pre><code>export(&quot;%notin%&quot;)
</code></pre>
</div>
<h2>The Rd file</h2>
<p>This is where things get are a bit more interesting. The LaTeX engine needs the percent sign to be escaped (<code>\%</code>) throughout the whole documentation. Also pay close attention to the usage section (<code>x \%notin\% table</code>).</p>
<div>
<pre><code>\name{\%notin\%}
\alias{\%notin\%}
\title{
Negated Value Matching
}
\description{
\code{\%notin\%} is the negation of \code{\link{\%in\%}}, 
which returns a logical vector indicating if there is a non-match or not 
for its left operand.
}
\usage{
x \%notin\% table
}
\arguments{
  \item{x}{
vector or \code{NULL}: the values to be matched.
}
  \item{table}{
vector or \code{NULL}: the values to be matched against.
}
}
\value{
A logical vector, indicating if a non-match was located for each element of 
\code{x}: thus the values are \code{TRUE} or \code{FALSE} and never \code{NA}.
}
\author{
Peter Solymos &lt;solymos@ualberta.ca>
}
\seealso{
All the opposite of what is written for \code{\link{\%in\%}}.
}
\examples{
1:10 \%notin\% c(1,3,5,9)
sstr &lt;- c(&quot;c&quot;,&quot;ab&quot;,&quot;B&quot;,&quot;bba&quot;,&quot;c&quot;,NA,&quot;@&quot;,&quot;bla&quot;,&quot;a&quot;,&quot;Ba&quot;,&quot;\%&quot;)
sstr[sstr \%notin\% c(letters, LETTERS)]
}
\keyword{manip}
\keyword{logic}
</code></pre>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/how-to-write-and-document-special-functions-in-r/"></div></div><div><a data-url="https://www.r-bloggers.com/how-to-write-and-document-special-functions-in-r/" data-counturl="https://www.r-bloggers.com/how-to-write-and-document-special-functions-in-r/" data-count="vertical" data-text="How to write and document %special% functions in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://peter.solymos.org/code/2016/11/26/how-to-write-and-document-special-functions-in-r.html"> Peter Solymos - R related posts</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/agKDwBHxG4A"></a></div>