Select Page

How to Solve R Error: Unexpected else

by | Programming, R, Tips

This error occurs if you put the else statement on a new line after an if statement. R interprets the if statement as complete and does not expect an else.

You can solve this error by enclosing the entire if/else statement in braces/curly brackets {} or put the else clause on the same line as the end of the if-clause. If the if/else statement is in a function, you can enclose the entire body of the function in curly brackets or put the else clause on the same line as the end of the if-clause.

This tutorial will go through the error in detail and how to solve it with code examples.


If Else Statements in R

The if/else statement conditionally evaluates two statements. The syntax of the if/else statement is:

if (test_expression) { 
statement1
} else {
statement2
}

The else part is conditional, and R only evaluates it if the test_expression is FALSE.

When the if statement is not in a block, the else statement must appear on the same line as the end of statement1.

Otherwise, the new line at the end of statement1 completes the if, resulting in a complete statement to evaluate.

When R gets to the else statement it will interpret it as a completely new statement and not part of the preceding if statement.

Example

Consider the following function that takes an age as an argument and returns a statement depending on the age.

f <- function(age) 
  if (age >= 18) {
    print("Old enough to drive")
  }
  else 
       print("Not old enough to drive")
  

If the age is at least 18, the function returns “Old enough to drive”; otherwise, the function returns “Not old enough to drive”. Let’s run the code to see what happens:

Error: unexpected 'else' in "else"

The error occurs because the if-statement is a syntactically complete statement, and R does not know that the else clause belongs to the if clause above it.

Solution #1: Enclose the Entire Body of Function in Curly Brackets

The first way we can solve this error is by enclosing the entire body of the function in curly brackets. The else statement can be on the following line from the if clause in this case. Let’s look at the revised code:

f <- function(age) {
  if (age >= 18) {
    print("Old enough to drive")
  }
  else 
       print("Not old enough to drive")
  }

Let’s call the function to get the result:

f(18)
[1] "Old enough to drive"

We successfully called the function, which returned a print statement to the console.

Solution #2: Put the else statement on the same line as the ending curly bracket

The second way we can solve this error is to put the else statement on the same line as the ending curly bracket of the if-clause. Let’s look at the revised code:

f <- function(age) 
  if (age >= 18) {
    print("Old enough to drive")
  } else 
       print("Not old enough to drive")
  

Let’s call the function to get the result:

f(17)
[1] "Not old enough to drive"

We successfully called the function, which returned a print statement to the console.

Solution #3: If/Else on Single line

We can put the if/else statements on a single line, though this is less readable than curly brackets and new line breaks. Let’s look at the revised code:

f <- function (age) if (age >= 18) print("Old enough to drive") else print("not old enough to drive")

Let’s call the function to get the result:

f(18)
[1] "Old enough to drive"

We successfully called the function, which returned a print statement to the console.

Note that the above function syntax is the same as the following:

f <- function(age)
  if (age >= 18)
  print("Old enough to drive") else
  print("Not old enough to drive")

In other words, you do not need curly brackets, provided the else clause is on the same line as the end of the if clause. Let’s call the function to get the result:

  f(17)
[1] "Not old enough to drive"

Example #2

The use of a function is not necessary for if/else statements. The use of curly brackets and the location of the else clause is what matters for R to interpret the if/else statement correctly. Let’s look at an example of an if/else statement:

age <- 18

if (age >=18) {
print("Old enough to drive")
}
else print("Not old enough to drive")

Let’s run the code to see what happens:

[1] "Old enough to drive"
Error: unexpected 'else' in "else"

Note that R evaluates the if statement to TRUE and prints the first statement. When R reaches the else statement, it is an entirely new statement, not attached to an if clause. Therefore, R raises the error.

Solution #1: Use Curly Brackets

We can solve this error by wrapping the if-else statement in curly brackets.

age <- 17
{
if (age >=18) {
print("Old enough to drive")
}
else print("Not old enough to drive")
}

Let’s run the code to get the result:

[1] "Not old enough to drive"

Solution #2: Put Else-clause on Same Line as End of If-clause

We can also solve the error by putting the else-clause on the same line as the end of the if-clause:

age <- 17

if (age >=18) {
print("Old enough to drive")
} else print("Not old enough to drive")

Let’s run the code to get the result:

[1] "Not old enough to drive"

Summary

Congratulations on reading to the end of this tutorial! 

For further reading on R related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!