Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if/else constructs inside and outside functions

Tags:

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.

like image 663
Mark Heckmann Avatar asked Dec 05 '12 13:12

Mark Heckmann


People also ask

Can you put an if statement inside a function r?

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 .

Which one is the correct representation of if else command?

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.

Can we use if else in for loop?

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.

How do you start writing an if statement in R?

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.


1 Answers

Executive Summary:

There are only two ways for R to know that an else clause belongs to the if clause above it:

  1. The entire if...else statement (and perhaps other statements as well) is enclosed in braces;
  2. The word else appears on the same line as the end of the if clause.

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 } 
like image 65
Jerry Utah Avatar answered Oct 21 '22 18:10

Jerry Utah