I understand the usual way to write an "if - else if" statement is as follow:
if (2==1) {
print("1")
} else if (2==2) {
print("2")
} else {
print("3")
}
or
if (2==1) {print("1")
} else if (2==2) {print("2")
} else print("3")
On the contrary, If I write in this way
if (2==1) {
print("1")
}
else if (2==2) {
print("2")
}
else (print("3"))
or this way:
if (2==1) print("1")
else if (2==2) print("2")
else print("3")
the statement does NOT work. Can you explain me why }
must precede else
or else if
in the same line? Are there any other way of writing the if-else if-else statement in R, especially without brackets?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…