R Error: Unexpected else in “else” (2 Examples)

 

In this R tutorial you’ll learn how to solve the error unexpected else in “else”.

The table of content looks as follows:

Let’s just jump right in.

 

Example 1: Reproduce the Error – unexpected else in “else”

The following code illustrates how to replicate the error message unexpected else in “else” in R. Let’s assume we want to create an if else statement:

if(TRUE) {        # Reproducing the error
  "x"
}
else {
  "y"
}
# Error: unexpected 'else' in "else"

Gosh! We received an error message!

The reason for this error is that we didn’t write the else statement to the same line as the closing curly bracket of the if statement.

I’ll show you how to fix this issue in the following example…

 

Example 2: Fix the Error – unexpected else in “else”

Example 2 explains how to deal with the error message unexpected else in “else”. Let’s again run our if else statement, but this time we are executing the else statement one line higher:

if(TRUE) {        # Fixing the error
  "x"
} else {
  "y"
}
# [1] "x"

Works fine!

 

Video, Further Resources & Summary

Do you need further info on the R codes of this tutorial? Then you might want to have a look at the following video of my YouTube channel. In the video, I illustrate the R programming codes of this article.

 

The YouTube video will be added soon.

 

Furthermore, you might want to read the other articles on my homepage.

 

This article showed how to get rid of the error message unexpected else in “else” in the R programming language. Let me know in the comments below, in case you have further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


6 Comments. Leave new

  • #no is divisible by 2 or 3
    x=99
    if(x%%2==0)
    {
    print(“x is divisible by 2”)
    }
    else if(x%%3==0)
    {
    print(“x is divisible by 3”)
    }
    else
    {
    print(“no is neither divisible by 2 nor by 3”)
    }
    when we run this code, r studio will give output as a error which is written below
    Error: unexpected ‘else’ in “else”
    please solve my problem

    Reply
    • Hey Farhan,

      I think the way how you are using escape in your code is flawed.

      Please try the following R code:

      x=99
      if(x%%2==0) {
        print("x is divisible by 2")
      } else if(x%%3==0) {
        print("x is divisible by 3")
      } else {
        print("no is neither divisible by 2 nor by 3")
      }

      Regards,
      Joachim

      Reply
  • Hi,

    x <- -1

    if (x < 0){
    print("x is a negative number")
    } else if (x == 0) {
    print(" x is zero number")
    } else {
    print ("x is a positive number")
    }

    I am getting the error "unexpected 'else'
    16: }
    17: else"

    Kindly help.

    Regards,
    Nandita

    Reply
  • thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top