63

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Lucia
  • 901
  • 1
  • 11
  • 16
  • 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. – sebastian-c Feb 13 '13 at 23:50
  • 1
    Possible duplicate of http://stackoverflow.com/questions/13724063/if-else-constructs-inside-and-outside-functions – Ari B. Friedman Feb 14 '13 at 10:49

3 Answers3

93

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}
sebastian-c
  • 15,057
  • 3
  • 47
  • 93
  • 1
    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. – Andrew Schulman Jun 25 '19 at 17:31
  • 2
    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. – sebastian-c Jun 28 '19 at 14:00
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)
nadizan
  • 1,323
  • 10
  • 23
  • Just when I thought that I should add the rewritten code to be nice, I realized the answer already been answered by sebastian-c .. – nadizan Feb 14 '13 at 00:07
  • 1
    Sorry about that. I did appreciate the link to the language definition, though. It does answer the question. – sebastian-c Feb 14 '13 at 00:17
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.

Jacques Wainer
  • 527
  • 1
  • 5
  • 14