Chapter 9 Caution with parsing statements
What do you think will happen if we try the code below?
if(2+2) == 4
print("Arithmetic works.")
else
print("Houston, we have a problem.")
## Error: <text>:1:9: unexpected '=='
## 1: if(2+2) ==
## ^
This does not work because R
evaluates the first line and does not know that you are going to use an else
statement
Use curly brackets { }
so that R
knows to expect more input. Try:
if (2 + 2 == 4) {
print("Arithmetic works.")
else {
} print("Houston, we have a problem.")
}
## [1] "Arithmetic works."