Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - using "next" statement in apply function

Tags:

r

lapply

It looks like the next statement (used to stop the current evaluation and go to the next iteration of a for-loop) doesn't work inside an apply function.

Example: lapply(1:10, function(x) if (x == 5) {next} else {print(x)})

Any convenient substitution for next in apply?

like image 845
Gratien Avatar asked Jan 04 '16 19:01

Gratien


People also ask

How do I use the next function in R?

Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination. 'next' is a loop control statement just like the break statement.

What does apply () do in R?

The apply() function lets us apply a function to the rows or columns of a matrix or data frame. This function takes matrix or data frame as an argument along with function and whether it has to be applied by row or column and returns the result in the form of a vector or array or list of values obtained.

What is the use of next statement in R?

The next statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it. On encountering next, the R parser skips further evaluation and starts next iteration of the loop.

How use next and break in R?

The basic Function of Break and Next statement is to alter the running loop in the program and flow the control outside of the loop. In R language, repeat, for and while loops are used to run the statement or get the desired output N number of times until the given condition to the loop becomes false.

What is the use of next statement in R?

The next statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it. On encountering next, the R parser skips further evaluation and starts next iteration of the loop. The basic syntax for creating a next statement in R is −

How do you use next in a loop in R?

A next statement is useful when we want to skip the current iteration of a loop without terminating it. On encountering next, the R parser skips further evaluation and starts next iteration of the loop. The syntax of next statement is: if (test_condition) { next }

What is the use of break and next in R?

The basic Function of Break and Next statement is to alter the running loop in the program and flow the control outside of the loop. In R language, repeat, for and while loops are used to run the statement or get the desired output N number of times until the given condition to the loop becomes false.

What is the flowchart for the next statement in R?

The flowchart for the next statement in R programming represents a generic loop scenario. It starts with a loop condition check followed by if statement and if condition check. The if statement block associated with the next statement. The next statement scopes the particular iteration and manages the control flow of the loop.


1 Answers

The short answer for cases like yours is return.

lapply(1:10, function(x) if (x == 5) {return()} else {print(x)})

Makes sense since the code you're applying is a function. Obviously I'm assuming your real case is more complex, and it wouldn't work to simply omit the true arm of the if.

like image 192
Nat Goodman Avatar answered Sep 21 '22 16:09

Nat Goodman