What do the %op% operators in mean? For example "%in%"?

48,382

Solution 1

Put quotes around it to find the help page. Either of these work

> help("%in%")
> ?"%in%"

Once you get to the help page, you'll see that

‘%in%’ is currently defined as

‘"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0’


Since time is a generic, I don't know what time(X2) returns without knowing what X2 is. But, %in% tells you which items from the left hand side are also in the right hand side.

> c(1:5) %in% c(3:8)
[1] FALSE FALSE  TRUE  TRUE  TRUE

See also, intersect

> intersect(c(1:5), c(3:8))
[1] 3 4 5

Solution 2

I didn't think GSee's or Sathish's answers went far enough because "%" does have meaning all by itself and not just in the context of the %in% operator. It is the mechanism for defining new infix operators by users. It is a much more general issue than the virtues of the %in% infix operator or its more general prefix ancestor match. It could be as simple as making a pairwise "s"(um) operator:

 `%s%` <- function(x,y) x + y

Or it could be more interesting, say making a second derivative operator:

 `%DD%` <- function(expr, nam="x") { D(D( bquote(.(expr)), nam), nam) }
 expression(x^4) %DD% "x"
 # 4 * (3 * x^2)

The %-character also has importance in the parsing of Date, date-time, and C-type format functions like strptime, formatC and sprintf.

Since that was originally written we have seen the emergence of the magrittr package with the dplyr elaboration that demonstrates yet another use for %-flanked operators.

So the most general answer is that % symbols are handled specially by the R parser. Since the parser is used to process plotmath expressions, you will also see extensive options for graphics annotations at the ?plotmath help page.

Solution 3

%op% denotes an infix binary operator. There are several built-in operators using %, and you can also create your own.

(A single % sign isn't a keyword in R. You can see a list of keywords on the ?Reserved help page.)


How do I get help on binary operators?

As with anything that isn't a standard variable name, you have to to enclose the term in quotes or backquotes.

?"%in%"
?`%in%`    

Credit: GSee's answer.


What does %in% do?

As described on the ?`%in%` help page (which is actually the ?match help page since %in% is really only an infix version of match.),

[%in%] returns a logical vector indicating if there is a match or not for its left operand

It is most commonly used with categorical variables, though it can be used with numbers as well.

c("a", "A") %in% letters
## [1]  TRUE FALSE

1:4 %in% c(2, 3, 5, 7, 11)
## [1] FALSE  TRUE  TRUE FALSE

Credit: GSee's answer, Ari's answer, Sathish's answer.


How do I create my own infix binary operators?

These are functions, and can be defined in the same way as any other function, with a couple of restrictions.

  1. It's a binary opertor, so the function must take exactly two arguments.
  2. Since the name is non-standard, it must be written with quotes or backquotes.

For example, this defines a matrix power operator.

`%^%` <- function(x, y) matrixcalc::matrix.power(x, y)

matrix(1:4, 2) %^% 3

Credit: BondedDust's answer, Ari's answer.


What other % operators are there?

In base R:

%/% and %% perform integer division and modular division respectively, and are described on the ?Arithmetic help page.

%o% gives the outer product of arrays.

%*% performs matrix multiplication.

%x% performs the Kronecker product of arrays.

In ggplot2:

%+% replaces the data frame in a ggplot.

%+replace% modifies theme elements in a ggplot.

%inside% (internal) checks for values in a range.

%||% (internal) provides a default value in case of NULL values. This function also appears internally in devtools, reshape2, roxygen2 and knitr. (In knitr it is called %n%.)

In magrittr:

%>% pipes the left-hand side into an expression on the right-hand side.

%<>% pipes the left-hand side into an expression on the right-hand side, and then assigns the result back into the left-hand side object.

%T>% pipes the left-hand side into an expression on the right-hand side, which it uses only for its side effects, returning the left-hand side.

%,% builds a functional sequence.

%$% exposes columns of a data.frame or members of a list.

In data.table:

%between% checks for values in a range.

%chin% is like %in%, optimised for character vectors.

%like% checks for regular expression matches.

In Hmisc:

%nin% returns the opposite of %in%.

In devtools:

%:::% (internal) gets a variable from a namespace passed as a string.

In sp:

%over% performs a spatial join (e.g., which polygon corresponds to some points?)

In rebus:

%R% concatenates elements of a regex object.


More generally, you can find all the operators in all the packages installed on your machine using:

library(magrittr)

ip <- installed.packages() %>% rownames
(ops <- setNames(ip, ip) %>% 
  lapply(
    function(pkg)
    {
      rdx_file <- system.file("R", paste0(pkg, ".rdx"), package = pkg)
      if(file.exists(rdx_file))
      {
        rdx <- readRDS(rdx_file)
        fn_names <- names(rdx$variables)
        fn_names[grepl("^%", fn_names)]
      }
    }
  ) %>% 
  unlist
)

Solution 4

More generally, %foo% is the syntax for a binary operator. Binary operators in R are really just functions in disguise, and take two arguments (the one before and the one after the operator become the first two arguments of the function).

For example:

> `%in%`(1:5,4:6)
[1] FALSE FALSE FALSE  TRUE  TRUE

While %in% is defined in base R, you can also define your own binary function:

`%hi%` <- function(x,y) cat(x,y,"\n")
> "oh" %hi% "my"
oh my 

Solution 5

%in% is an operator used to find and subset multiple occurrences of the same name or value in a matrix or data frame.

For example 1: subsetting with the same name

set.seed(133)
x <- runif(5)
names(x) <- letters[1:5]
x[c("a", "d")]
#  a         d 
#  0.5360112 0.4231022

Now you change the name of "d" to "a"

 names(x)[4] <- "a"

If you try to extract the similar names and its values using the previous subscript, it will not work. Notice the result, it does not have the elements of [1] and [4].

x[c("a", "a")]

#        a         a 
#    0.5360112 0.5360112 

So, you can extract the two "a"s from different position in a variable by using %in% binary operator.

names(x) %in% "a"
#  [1]  TRUE FALSE FALSE  TRUE FALSE

#assign it to a variable called "vec"
 vec <- names(x) %in% "a"

#extract the values of two "a"s
 x[vec]
 #         a         a 
 #  0.5360112 0.4231022 

Example 2: Subsetting multiple values from a column Refer this site for an example

Share:
48,382

Related videos on Youtube

heavy rocker dude
Author by

heavy rocker dude

Updated on January 01, 2021

Comments

  • heavy rocker dude
    heavy rocker dude over 3 years

    I tried to do this simple search but couldn't find anything on the percent (%) symbol in R.

    What does %in% mean in the following code?

    time(x) %in% time(y) where x and y are matrices.

    How do I look up help on %in% and similar functions that follow the %stuff% pattern, as I cannot locate the help file?

    Related questions:

  • heavy rocker dude
    heavy rocker dude over 11 years
    So does: which(time(X2)%in%time(Y)) basically return the greater date between X2 and Y? Thanks to both for the answers
  • Sathish
    Sathish over 11 years
    another such user defined binary operator is %*% which does true matrix multiplication, whereas the operator * does only vectorized computation of data.
  • Ari B. Friedman
    Ari B. Friedman over 11 years
    @Sathish Noted, although I'd call %*%, %in%, etc. "non-user-defined" or "built-in" operators.
  • Coruscate5
    Coruscate5 almost 7 years
    This straightforwardness of this answer was surprisingly helpful, despite understanding the higher rated answers
  • smci
    smci over 5 years
    Superb summary. This should be the accepted answer.
  • vasili111
    vasili111 about 5 years
    Great explanation! Also, useful link: datamentor.io/r-programming/infix-operator Do you know any official documentation with an explanation of inflix operators?
  • IRTFM
    IRTFM about 5 years
    Search in the R Language Definition for "Operators", "Group methods", "Special operators", and "Infix and prefix operators".
  • ESL
    ESL almost 3 years
    This does not answer original question, about %op% (custom infix operator), just answered the meaning of the example (what is not asked). 🤷🏻‍♂️🤦🏻‍♂️