“else"中出现意外的“else"错误 [英] Unexpected 'else' in "else" error
问题描述
我收到此错误:
错误:else"中出现意外的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 读取第一行,因为它是完整的,所以完整地运行它.当它到达下一行时,它会说否则?否则什么?"因为这是一个全新的声明.为了让 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屋!