Home > front end >  difference between `%in%` and `in` operator in R
difference between `%in%` and `in` operator in R

Time:10-19

What's the difference between the in and the %in% operator in R? Why do I sometimes need the percentage signs and other times I do not?

CodePudding user response:

The 3 following objects are all functions :

  • identity
  • %in%
  • for

We can call them this way :

`identity`(1)
#> [1] 1
`%in%`(1, 1:2)
#> [1] TRUE
`for`(x, seq(3), print("yes"))
#> [1] "yes"
#> [1] "yes"
#> [1] "yes"

But usually we don't!

"identity" is syntactic (i.e. it's a "regular" name, doesn't contain weird symbols etc), AND it is not a protected word so we can skip the tick marks and call just :

identity(1)

%in% is not syntactic but it starts and ends with "%" so it can be used in infix form. you could define your own `%fun%` <-function(x,y) ... and use it this way to, so we would call :

1 %in% 1:2

for is a control flow construct, like if, while and repeat, all of those are functions with a given number of arguments, but they come in the language with more convenient ways to call them than the above. here we'd do :

for (x in seq(3)) print("yes")

in is just used to parse the code, it's not a function here (just like else isn't either.

?`%in%` will show you what the function does.

CodePudding user response:

Depending on how you define it, there is no in operator in R, only an %in% operator. Instead, in is “syntactic sugar” as part of the syntax for the for loop.

By contrast, %in% is an actual operator defined in R which tests whether the left-hand expression is contained in the right-hand expression. As other operators in R, %in% is a regular function and can be called as such:

if (`%in%`(x, seq(3, 5))) message("yes")

… or it can be redefined:

`%in%` = function (x, table) {
    message("I redefined %in%!")
    match(x, table, nomatch = 0L) > 0L
}

if (5 %in% 1 : 10) message("yes")

# I redefined %in%!
# yes

CodePudding user response:

Usage-wise, I have figured out the answer: I can only use in when I loop through everything, and %in%for checking whether something is contained in something else, e.g.

for (x in seq(3)){
 if (x %in% seq(3,5)) print("yes")
}
  •  Tags:  
  • r
  • Related