“else”中出现意外的“else”错误 [英] Unexpected 'else' in "else" error
问题描述
我收到此错误:
错误:else中意外的其他
Error: unexpected 'else' in " else"
从这个 if,else
语句:
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) } }
什么这个有问题吗?
推荐答案
你需要重新排列大括号。您的第一个语句已完成,因此R将其解释为此类并在其他行上生成语法错误。您的代码应如下所示:
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
然后R读取第一行,因为它完整,完整地运行。当它到达下一行时,它会变成Else?还有什么?因为这是一个全新的陈述。要让R将else解释为前面if语句的一部分,你必须使用大括号告诉R你还没有完成:
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}
这篇关于“else”中出现意外的“else”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!