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?
Answer 50aa6266db2df2c0d8006d0fRemove the semi-colon after specifying the condition in () in the first If statement. Correct syntax is : if (yourName. length>0 && gender.
If you are getting an error about the else it is because you've told the interpreter that the ; was the end of your if statement so when it finds the else a few lines later it starts complaining. A few examples of where not to put a semicolon: if (age < 18); if ( 9 > 10 ); if ("Yogi Bear". length < 3); if ("Jon".
Example 1: Reproduce the Error – unexpected else in “else” We received an error message! The reason for this error is that we didn't write the else statement to the same line as the closing curly bracket of the if statement.
This error means that Java is unable to find an if statement associated to your else statement. Else statements do not work unless they are associated with an if statement. Ensure that you have an if statement and that your else statement isn't nested within your if statement.
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}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With