When I look at R functions I often find the following structure:
f <- function(exp=T) { if (exp) a <- 1 else a <- 2 } f() f(F)
This will run without error. But executing the inner function code throws an error as R probably assumes that the statement is finished after the first assignment a <- 1
and cannot handle the following else.
exp=T if (exp) a <- 1 else a <- 2
Now, this makes sense to me, but I still would like to understand why the behaviour of the executed code differs when executed inside or outside a function.
To run an if-then statement in R, we use the if() {} function. The function has two main elements, a logical test in the parentheses, and conditional code in curly braces. The code in the curly braces is conditional because it is only evaluated if the logical test contained in the parentheses is TRUE .
Syntax of ifelse() function This is to say, the i-th element of result will be x[i] if test_expression[i] is TRUE else it will take the value of y[i] . The vectors x and y are recycled whenever necessary.
You can nest If statements inside For Loops. For example you can loop through a list to check if the elements meet certain conditions. You can also have a For Loop inside another For loop.
Syntax. if(boolean_expression) { // statement(s) will execute if the boolean expression is true. } else { // statement(s) will execute if the boolean expression is false. } If the Boolean expression evaluates to be true, then the if block of code will be executed, otherwise else block of code will be executed.
Executive Summary:
There are only two ways for R to know that an else clause belongs to the if clause above it:
Evidence:
The above discussion helped me, but I hope I can offer a useful quibble. Yes, it is correct that
f <- function (exp) if (exp) 1 else 2
fails with the classic Error: unexpected 'else' in "else"
message due to R's failure to keep reading past the 1. Two ways have been correctly offered to make R keep reading past the 1:
f <- function (exp) { if (exp) 1 else 2 }
and
f <- function (exp) if (exp) 1 else 2
But there is a third way not yet mentioned---just move else
up a line. Thus, the following also works because R knows to keep reading past the 1:
f <- function (exp) if (exp) 1 else 2
I think the key point is to either brace the entire body of the function, or make sure the else
occurs on the same line as the end of the if clause so that R knows to keep reading. That's why the one-line solution works. It is also why this works:
f <- function (exp) if (exp) { 1 } else 2
But this fails:
f <- function (exp) if (exp) { 1 } else 2
And using the more standard bracing of the function body, this works too:
f <- function (exp) { if (exp) { 1 } else 2 }
But whether or not we are building a function is a red herring. What matters is only braces and location of else
. Thus, these work:
{ if (exp) { 1 } else 2 } if (exp) { 1 } else 2
but this fails:
if (exp) { 1 } else 2
and to demonstrate my assertion 1 at the top, this works:
{ x <- 4 if (exp) 1 else 2 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With