Back to main workshop page

Objectives:

  • Review use of logic in R with logical indexing
  • Learn why we might want to use an “if” statement
  • Think about how an “if” statement is used differently in comparison to a function
  • Learn the structure of a simple if statements and apply it.
  • Add complexity to our logic with & and (and, or)
  • Add an “else” statement so that an alternative happens when the statement is false.
  • Work with “else if” statements to add further complexity to the code.

Reviewing Logic:

Let’s go back over some of the “logical indexing” and “logical operations” that we covered two weeks ago:

weight <- 10
# Is weight bigger than 5?
weight >5 #TRUE
# Is weight less than 5?
weight <5 #FALSE
# is weight less than or equal to 10?
weight <=10 #TRUE
# is weight equal to 10?
weight ==10 #TRUE
# is weight "not equal" to 10?
weight !=10 #FALSE
# also works on strings:
animal = 'cat'
animal == 'cat' #TRUE
animal != 'cat' #FALSE
Symbol Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== equal to
!= Not equal to

Introducing if statements

Instead of using these like we did last time with logical indexing, we are going to use these logical operators to have R choose what code to run. These are called if statements and look like this:

if(Q GOES HERE){
  # code only runs if logic is TRUE
}

Let’s give this a try with a single weight, using print:

# The function "print" prints text out in the console:
print('text')
if(weight == 10){
  'The weight is 10.''
}

Challenge 1:

Write an if statement that prints “Num is positive”, when the value of the variable num is positive.

Answer:

num = 10
if(num>0){
  print('Num is positive')
}

Adding additional complexity with “and”, “or”…

R knows how to use these logical operators on both strings and numbers. However we can combine these logical operators using two commands: & and ** **.

Let’s start with &:

Both things must be true:

TRUE & FALSE
TRUE & TRUE
FALSE & TRUE
weight==10 & weight <10
weight==10 & weight<=10
Now with or, ** **, Only one of the things have to be true:
TRUE | FALSE
TRUE | TRUE
FALSE | TRUE
weight==10 | weight <10
weight==10 | weight<=10

Challenge:

  • write a logical operator that prints “the number is within 2 of 0”. if the number is between -2 and 2.

Answer:

if(num>-2 & num<2){
  print('the number is within 2 of zero.')
}

An alternative happens when the statement is false:

Currently, things only happen when the statement in the if() statement is true. One handy aspect of if statements, though, is the addition of an “else statement”.

if(logical operator){
  # This code runs if the logical operator is true.
}else{
  # This code runs if the logical operator is false.
}

Let’s set up the previous if statement so that it also prints “Num is Negative” when num>0 is false?

num = 10
if(num>0){
  print('Num is positive')
}else{
  print('Num is negative')
}

Whenever you write a piece of code like this, you should think of all of the possible numeric inputs and make sure that the code does as you intended. In this case, we want to make sure that the this code prints the right output no matter what num is.

  • What’s the output when num is 20? In this case the first if statement is true, and thus prints Num is negative. Then the second part of the code doesn’t run.
  • What about num <- -20? The first if statement is false. Because of that, only the second print() is run.
  • What about num <- 0? The first if statement is false, so the second print() is run.

Do you see the issue now? if num were 0, this code will print “num is negative”. In this case can we set up a third case where zero would print Num is zero?

num = 0
if(num>0){
  print('Num is positive')
}else if (num<0){
  print('Num is negative')
else{
  print('Num is zero')
}

In the above case, if the number is greater than zero, it prints the first print line like usual. Then, for the remaining numbers (numbers up through 0), it tries the second if statement (num<0). If that is true, it prints “Num is negative”, but if it isn’t true, the only value remaining would be zero. So The last print() statement runs.

Challenge:

Make a function called “evenify” that takes a single numerical input.

If the input is even, the function returns the number without any difference. If the input is odd, the function returns the number plus one.

e.g.

evenify(22) # returns 22
evenify(23) # returns 24

hint: Getting the remainder of a number in coding is called “modulus”. In R it is designated as %%.

5 %% 2 # returns 1
9 %% 5 # returns 4.