Difference between logical operators '|' and '||' in R

r
operators

#1

Hello,

What is the difference between the logical OR operators ‘|’ and ‘||’ in R? I am having some problem in creating a new variable in my dataset due to the wrong usage of these logical operators. Can I get an explanation with an example?

Thanks.


#2

@mukesh, I don’t think there is any difference between the two. (See this, for example.) Update: There actually is; see the next post.

What exactly is the problem that you’re facing?

> x = 5
> x > 2 || x < 3
[1] TRUE
> x > 2 | x < 3
[1] TRUE
> x < 2 | x < 3
[1] FALSE
> x < 2 || x < 3
[1] FALSE
> x < 2 || x < 3 || x >= 4
[1] TRUE

#3

Wrong. There is a very big difference. Check here and here.


#4

Thanks. Lesson learnt for the day. :smile: