How to Solve the R Error – Unexpected else in Code (2 Examples)

In this tutorial, I’ll illustrate how to get rid of the error message unexpected else in “else” in R programming.

Example 1: Reproducing Error Message: unexpected else in “else”

else               # How to replicate the error message
# Error: unexpected 'else' in "else"

Example 2: Solving Error Message: unexpected else in “else”

if(FALSE) {        # How to fix the error message
  1:5
} else {
  5:1
}
# [1] 5 4 3 2 1

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.
You need to agree with the terms to proceed

Menu
Top