logical
logical
ComparisonsThis may seem tedious, but understanding how to use logical
operators and functions is essential to writing code.
We often want to evaluate whether an object is equal to another object.
logical
operator returns TRUE
logical
operator returns FALSE
We can check whether two objects are equal or not by using ==
and !=
==
checks to see if two objects are equal to each other.!=
checks to see if two ojects are not equal to each other.logical
operators can be used to compare all kinds of objects.
1 == 1 # numeric objects
## [1] TRUE
"a" != "c" # character objects
## [1] TRUE
c(1, "a", 3) == c(1, 2, "b") # combined objects
## [1] TRUE FALSE FALSE
TRUE == FALSE # even `logical` objects!
## [1] FALSE
1 == TRUE # 1 actually means the same as TRUE
## [1] TRUE
0 == FALSE # 0 actually means the same as FALSE, we'll actually use this characteristic later
## [1] TRUE
We can check if numeric
objects are lesser than or greater than other numeric
values
>
checks if the object to the left is greater than the object to the right<
checks if the object to the left is lesser than the object to the right>=
checks if the object to the left is greater than OR equal to the object to the right<=
checks if the object to the left is lesser than OR equal to the object to the right1 < 2
## [1] TRUE
2 < 1
## [1] FALSE
3 >= 3
## [1] TRUE
4 <= 2
## [1] FALSE
logical
Comparisonslogical
operators can be combined with:
&
, meaning “and”, is used when you want to check more than one comaprison where ALL comparisons must retun TRUE
for the entire statement to be TRUE
.1 == 1 & 2 != 3
## [1] TRUE
2 != 3 & 5 == 6
## [1] FALSE
|
, meaning “or”, is used when you want to check more than one comparison where ANY comparison returns TRUE
1 == 2 | 3 != 2
## [1] TRUE
"tracie" != "4" | "bill" != "joe"
## [1] TRUE
If you’re coming from other languages, you may be tempted to use &&
or ||
and at first glance they may appear to do the same thing, but this isn’t exactly true. You can use ?"&"
to learn more.
So what? Using logical
operators combined with if()
statements let’s us create instructions for our program. Here is a really simple example of how if()
statement works.
if(1 == 1){
print("1 is equal to 1!")
}
## [1] "1 is equal to 1!"
if()
What happens here is that if()
checks the logical
comparison inside of the parentheses. If it evaluates to TRUE
then runs the code inside { }
. If not, it does nothing.
We can make if()
statements more complicated by using else
. Here’s an example where we create two variables for our comparison, because in reality that’s closer to what you’d likely be using.
a_car <- "a car"
tom <- "tom"
if(tom == a_car){
print("this print statement isn't going to do anything")
} else {
print("tom is not a car, that would be weird")
}
## [1] "tom is not a car, that would be weird"
Here, if()
checks if tom
equals a_car
, which evaluates to FALSE
, so it moves to the else
statement and executes the code in else
’s { }
.
else if()
You can make your if()
statement more complicated, and thus potentially more useful by adding else if()
statements.
a_plane <- "a plane"
if(tom == a_plane){
print("this print statement isn't going to do anything")
} else if(tom != a_plane) {
print("tom is not a plane, that would also be weird")
} else {
print("this print statement isn't going to do anything")
}
## [1] "tom is not a plane, that would also be weird"
What happens here is that if()
checks if tom
equals a_car
, which evaluates to FALSE
and R
moves on the next line where else if()
checks if tom
does not equal a_plane
, which evaluates to TRUE
and the code inside else if()
’s { }
is executed.
else
Guess what would happen if neither if()
nor else if()
evaluates to TRUE
when you finish with an else
statement. You can probably guess, but you can also just give it a try.
%in%
You might be thinking that if you want to check if a value is equal to any of the values contained in a collection of objects that you’d need to use a whole bunch of if()
statements.
While that is possible, R
gives us a trick do just that with the %in%
operator. It’s used just as you might expect, where it it checks to see if an object is %in%
another object that is a collection.
Let’s say that we had a list of the people eating lunch in the Sampson Center, which we store in a variable called sampson_center
.
sampson_center <- c("lauren", "joey", "cyril", "bertha")
Then, if we want to check if "zack"
or "cyril"
are in sampson_center
we would use %in%
.
"zack" %in% sampson_center
## [1] FALSE
"cyril" %in% sampson_center
## [1] TRUE
ifelse()
We already saw how if()
and else
can work, but R
gives a really simple way to combine them into one function with ifelse()
.
ifelse()
is an elegent way of checking if things are TRUE
and doing something based on that result.
ifelse()
, your first argument is the logical
evaluation (test
) in which you’re interested, like "zack" %in% sampson_center
above.yes
) is what you want to happen if your logical
evaluation returns TRUE
.no
) is what you want to happen if your logical
evaluation returns FALSE
.You can also use <-
with ifelse()
when you want to assign a value based on a logical
evaluation. Let’s make a variable named zack_status
to which we’ll assign "present"
or "absent"
based on whether or not "zack"
is in sampson_center
. We’ll also do the same for "lauren"
with a variable called lauren_status
.
zack_status <- ifelse("zack" %in% sampson_center, "present", "absent")
lauren_status <- ifelse("lauren" %in% sampson_center, "present", "absent")
zack_status
## [1] "absent"
lauren_status
## [1] "present"
ifelse()
is incredibly handy to know how to use, we just had to cover a bit of ground to get there. Now back to our network, where ifelse()
makes our life easy.
If that wasn’t clear or you want to know more, remember that you can type ?ifelse
to look at the documentation.