From the course: R for Data Science: Lunch Break Lessons

Logical operators

- [Instructor] R uses standard logical notation for or and and, and exclusive or. But let's spend a bit of time just to review how those functions work. First of all, the exclamation mark stands for not. So if I type in not true, I'll get false. Or surprisingly if I type in not false I'll get true. It just inverts the value. Now if I were to type in something like quote apple, not apple, I get an error because apple is not a logical value However, if I type in not two, I get false. Numbers greater than zero are true. If I type in not zero, I get true. Zero actually calculates out as a logical value of false. So not false equals true. The ampersand is for and. So I can type in true and true, which means if true and true then I get true. If I type in true and false then I'll receive false because both arguments have to be true in order for the result to be true. Now I'm going to create two vectors, I'm going to call the first one first vector false. And into first vector false I'm going to place a vector that contains false and true. I'm also going to create a vector called first vector true and into it I'm going to place a vector of true, comma, false. Now I'm going to use those and compare the results. So let's compare, first vector false and first vector false. And I get back two values because it's taking the first value of first vector false and comparing it to first vector false which we know them both to be false so it gives me the result of false. Then it goes back and compares the second value of first vector false against the second value of first vector false which is true and true. So it gives me a result of true. Now let's change that just a bit. Instead of comparing first vector false against first vector false. Let's compare first vector false and first vector true. Now we get is false and false. Well the first value of first vector false is false and the first value of first vector true is true but they both have to be true in order for the result to be true. Likewise for the second value. It's true and false, they both have to be true so the result again is false. Let's talk about the pipeline symbol. The pipeline symbol means or. So in this case I can type in true or true and I'm going to get back true. If I type in true or false, I'll get back true because or is saying, this value is true or this value is true and in this case, the first value is true so the result is true. If I again use first vector false against first vector true or you can see that I get true and true because the first values of first vector false and first vector true, one of those contains true and the second value, one of them contains true. Now let's talk about xor. An xor is either this or that. We'll use xor, it's a function, and to exclusive or, I will give true and the argument false. Now when I run this command, I get the value true. Which means that either the first argument is true or the second argument is true. So let's take a look at this. Xor, false, and true which again gives me true. However, if I do an exclusive or against true and true, what I get is, false. Because either the first argument is true or the second argument is true. Likewise, if I use xor and I compare false against false I'll get false. And that's because either the first argument is true, which in this case is not true or the second argument is true which in this case is not true and so we get false. So again, R uses standard logical notation such as exclamations and ampersands and pipes to indicate not and or. It uses a function to indicate xor.

Contents