if - else if - else statement and brackets
R reads these commands line by line, so it thinks you're done after executing the expression after the if statement. Remember, you can use if
without adding else
.
Your third example will work in a function, because the whole function is defined before being executed, so R knows it wasn't done yet (after if() do
).
In R, also we have ifelse() function:
ifelse(1 < 0, "hello", "hi")
Output:
# [1] "hi"