Skip to content

Conditionals and logical operators

Lucy M Chang edited this page Apr 3, 2018 · 41 revisions

Logical operators

We discussed logical operators previously. As a reminder, these allow you to create statements that result in TRUE or FALSE.

Common Logical Operators

< less than <= less than or equal to
> greater than >= greater than or equal to
== equals != does not equal
| or & and

if...

The function if() evaluates a condition - whatever statement is inside the parentheses. If it is TRUE, it then continues to perform the actions following it in curly brackets.

Essentially it asks the following:

print("Start run of code.")

if(TRUE){
  ### any code you want can be here ###
  print("Our 'if' statement is true.")
}

if(FALSE){
  ### any code you want can be here ###
  print("Our 'if' statement is false.")
}

print("End run of code.")

Exercise

Write a few lines of code, where if x is a number (any number), return that number plus 3. Let's get you started...

x <- 7

if(  ){

}

Test to make sure your code works. (Try different values for x.)

x <- 1000000000
x <- -4
x <- "word"

... else

The else statement. If the conditional is FALSE, then it performs the code following the curly bracket. We can rewrite the above code block to:

print("Start run of code.")

if(TRUE){
  print("Our 'if' statement is true.")
} else { # here we give the false condition some actions
  print("Our 'if' statement is false.")
}

print("End run of code.")

Exercise

Modify your previous if-statement (the one that adds three if x is a number) so that, if x is not a number, return its class instead

if... else if...

A combination else if() lets you list multiple conditions.

Note, all the statements are evaluated sequentially a.k.a. line by line.

x <- 42

# before you run the following code, answer the questions in the comments
if(x < 50){ # WILL THIS BE TRUE OR FALSE?
  print("Yes, x is less than 50.")
}else if(x > 35){ # WILL THIS BE TRUE OR FALSE?
  print("Yes, x is greater than 35.")
}else{
  print("None of our criteria were met.")
}

# what statement do you expect will print out in the console?

How would you nest these if statements? What happens to the output? What happens if you put else on a new line?

! (exclamation point)

The exclamation point ahead of a statement is a useful thing to know. It turns TRUE to FALSE and vice versa.

1:10

1:10 > 7

!(1:10 > 7) # what are the parentheses for?

# the statement "1 < 7" returns a TRUE
if(1 < 7){
  print("One is less than seven.")
}

# we flip that TRUE
if(!(1 < 7)){
  print("The conditional is FALSE, so this statement should not print.")
}

Exercise

We want to find out if any number in a vector (the first column of the R dataset iris) is greater than 7. Here's my first attempt:

if(iris[, 1] > 7){
  print("Found sepal length greater than 7.")
} else {
  print("Did NOT find sepal length greater than 7.")
}

Turn to a neighbor and discuss:

  • What happened? Did it work?
  • What are some ways this can be improved on? (Try out some other approaches.)

Next, find out which elements in the first column of the iris dataset are greater than 7.

Scripting best practices

Whenever you are trying to evaluate something, always go back and check that it's working. You can do this by:

  • using dummy data, especially to test the extremes
  • using subsets of your real data, so you can go back and verify the output