Unexpected 'else' in "else" error Unexpected 'else' in "else" error r r

Unexpected 'else' in "else" error


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 <- TRUEelse 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}


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)