37

I understand the usual way to write an "if - else if" statement is as follow:

if (2==1) {
  print("1")
} else if (2==2) {
  print("2")
} else {
  print("3")
}

or

if (2==1) {print("1") 
} else if (2==2) {print("2")
} else print("3")

On the contrary, If I write in this way

if (2==1) {
  print("1")
} 
else if (2==2) {
  print("2")
}
else (print("3"))

or this way:

if (2==1) print("1") 
else if (2==2) print("2")
else print("3")

the statement does NOT work. Can you explain me why } must precede else or else if in the same line? Are there any other way of writing the if-else if-else statement in R, especially without brackets?

zx8754
  • 52,746
  • 12
  • 114
  • 209
MasterJedi
  • 1,618
  • 1
  • 18
  • 17
  • 3
    When the initial `if` is followed by a compound expression (indicated by the `{}` pair) the parser by default is going to expect the expression followed by `else` to be compound as well. The only defined use of `else` is with compound expressions. This is even stated in the docs: `if(cond) cons.expr else alt.expr` where `cons.expr` and `alt.expr` are defined to be compound. As @Berry indicated, you can use the way R parses function definitions to work around this, but it'd be better to be consistent in bracket use (IMO). – hrbrmstr Sep 17 '14 at 11:31
  • also if you wrap the unusual if-else in brackets `{ bad if-else expr }` or in a function which is more common `function() { bad if-else expr}`, it will work – rawr Jul 03 '15 at 15:40
  • Possible duplicate of [Unexpected 'else' in "else" error](http://stackoverflow.com/questions/14865435/unexpected-else-in-else-error) – sebastian-c May 03 '16 at 17:36
  • I find it a pity that 'Reformat Code' (in RStudio) does not format the else towards the right place. It could see this issue, IMHO. – Victor Reijs Sep 23 '18 at 20:16

4 Answers4

41

R reads these commands line by line, so it thinks you're done after executing the expression after the if statement. Remember, you can use if without adding else.

Your third example will work in a function, because the whole function is defined before being executed, so R knows it wasn't done yet (after if() do).

zx8754
  • 52,746
  • 12
  • 114
  • 209
Berry Boessenkool
  • 1,506
  • 11
  • 15
14

In R, also we have ifelse() function:

ifelse(1 < 0, "hello", "hi")

Output:

# [1] "hi"
zx8754
  • 52,746
  • 12
  • 114
  • 209
Honey
  • 141
  • 1
  • 3
2

As hrbrmstr has mentioned:

When the initial if is followed by a compound expression (indicated by the {} pair) the parser by default is going to expect the expression followed by else to be compound as well. The only defined use of else is with compound expressions.

In the statement if(cond) cons.expr else alt.expr, the else needs to be put after and in same line with the end `cons.expr' compound.

So if you want to have your code a better look without brackets, apply this way:

if (2==1) print("1") else 
   if (2==2) print("2") else 
      print("3")
0

ifelse has tree parametes, first conditon, second true result and last false result.

y_pred = ifelse(prob_predict > 0.5,1,0)
Talha Rasool
  • 1,126
  • 14
  • 12