Unexpected 'else' in "else" error
Asked Answered
D

4

64

I get this error:

Error: unexpected 'else' in " else"

From this if, else statement:

if (dsnt<0.05) {
     wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) }
else {
      if (dst<0.05) {
wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) }
   else {
         t.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)       } }

What is wrong with this?

Drobman answered 13/2, 2013 at 23:47 Comment(2)
Your if statement is complete by the second line. Move the curly bracket at the end of it to the start of the 3rd line. Do the same for the 5th.Pallbearer
Possible duplicate of #13724563Vorfeld
P
95

You need to rearrange your curly brackets. Your first statement is complete, so R interprets it as such and produces syntax errors on the other lines. Your code should look like:

if (dsnt<0.05) {
  wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)
} else if (dst<0.05) {
  wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)
} else {
  t.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)       
} 

To put it more simply, if you have:

if(condition == TRUE) x <- TRUE
else x <- FALSE

Then R reads the first line and because it is complete, runs that in its entirety. When it gets to the next line, it goes "Else? Else what?" because it is a completely new statement. To have R interpret the else as part of the preceding if statement, you must have curly brackets to tell R that you aren't yet finished:

if(condition == TRUE) {x <- TRUE
 } else {x <- FALSE}
Pallbearer answered 13/2, 2013 at 23:56 Comment(2)
It's just odd that R requires this only when the if statement isn't inside a block. Inside of a block, you can write the else on its own and R will correctly associate it with the preceding if. Outside of a block, it won't. Weird.Rochelle
You're right, but I don't think it's that weird. When in a block, R can look ahead and see what comes next because all instructions are given at once, whereas in line by line it doesn't have a way to look ahead to the next line.Pallbearer
H
8

I would suggest to read up a bit on the syntax. See here.

if (dsnt<0.05) {
  wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE) 
} else if (dst<0.05) {
    wilcox.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)
} else 
  t.test(distance[result=='nt'],distance[result=='t'],alternative=c("two.sided"),paired=TRUE)
Homonym answered 13/2, 2013 at 23:51 Comment(2)
Just when I thought that I should add the rewritten code to be nice, I realized the answer already been answered by sebastian-c ..Homonym
Sorry about that. I did appreciate the link to the language definition, though. It does answer the question.Pallbearer
T
0

I dislike braces (too much python programming I guess). My solution for simple if else like

if(condition == TRUE) {x <- TRUE
 } else {x <- FALSE}

is

if(condition == TRUE) 
   x <- TRUE
else 
   x <- FALSE

is clear enough.

Towards answered 8/6, 2022 at 19:47 Comment(1)
Me, too. But gives unexpected token else on my machine, and, if executed, Error: unexpected 'else' in "else".Gonion
M
0

I just wanted to add that this doesn't happen if the if-else clauses are within a for-loop for some reason. You can have as many blank lines as you want between the end of the if-curly brackets and the else. I understand the explanation, but I find it extremely stupid that R doesn't just think "oh I thought the statement was complete, but since the next thing is 'else' I guess it's not," especially since that's apparently what it does in for-loops. So basically I can't style my code the way I like it in every case and have to change it depending on whether it's in a for-loop or not.

Magnifico answered 26/3, 2024 at 8:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.