I was wondering about how to write do-while-style loop?
I found this post:
you can use repeat{} and check conditions whereever using if() and
exit the loop with the "break" control word.
I am not sure what it exactly means. Can someone please elaborate if you understand it and/or if you have a different solution?
Pretty self explanatory.
repeat{
statements...
if(condition){
break
}
}
Or something like that I would think. To get the effect of the do while loop, simply check for your condition at the end of the group of statements.
See ?Control or the R Language Definition:
> y=0
> while(y <5){ print( y<-y+1) }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
So do_while does not exist as a separate construct in R, but you can fake it with:
repeat( { expressions}; if (! end_cond_expr ) {break} )
If you want to see the help page you cannot type ?while or ?repeat at the console but rather need to use ?'repeat' or ?'while'. All the "control-constructs" including if are on the same page and all need character quoting after the "?" so the interpreter doesn't see them as incomplete code and give you a continuation "+".
Building on the other answers, I wanted to share an example of using the while loop construct to achieve a do-while behaviour. By using a simple boolean variable in the while condition (initialized to TRUE), and then checking our actual condition later in the if statement. One could also use a break keyword instead of the continue <- FALSE inside the if statement (probably more efficient).
df <- data.frame(X=c(), R=c())
x <- x0
continue <- TRUE
while(continue)
{
xi <- (11 * x) %% 16
df <- rbind(df, data.frame(X=x, R=xi))
x <- xi
if(xi == x0)
{
continue <- FALSE
}
}
Noticing that user 42-'s perfect approach {
* "do while" = "repeat until not"
* The code equivalence:
do while (condition) # in other language
..statements..
endo
repeat{ # in R
..statements..
if(! condition){ break } # Negation is crucial here!
}
} did not receive enough attention from the others, I'll emphasize and bring forward his approach via a concrete example. If one does not negate the condition in do-while (via ! or by taking negation), then distorted situations (1. value persistence 2. infinite loop) exist depending on the course of the code.
In Gauss:
proc(0)=printvalues(y);
DO WHILE y < 5;
y+1;
y=y+1;
ENDO;
ENDP;
printvalues(0); # run selected code via F4 to get the following #
1.0000000
2.0000000
3.0000000
4.0000000
5.0000000
In R:
printvalues <- function(y) {
repeat {
y=y+1;
print(y)
if (! (y < 5) ) {break} # Negation is crucial here!
}
}
printvalues(0)
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
I still insist that without the negation of the condition in do-while, Salcedo's answer is wrong. One can check this via removing negation symbol in the above code.
Related
I would like to extract expr_a, expr_b, expr_c from the if/else block in the following function:
test <- function() {
result <- if (expr_a)
expr_b
else
expr_c
return(result)
}
for the purpose of dead-code elimination (I've seen the rco library but it currently doesn't support all the cases that I'm working with).
I would like to be able to then replace the entire loop with expr_b if eval(expr_a) == TRUE or expr_c otherwise.
The issue with regex-based approaches to this problem is that there are so many possible variations on this, and I'm not sure how to capture them all. For example, the code above is valid R code for one line expressions. Multi-line expressions surrounded by brackets can also be written e.g.:
else
{
# ...
}
Is there any way to identify and capture every possible if/else statement programmatically in R?
An example of what my real starting point looks like:
test <- function() {
my_var <- list(my_bool = FALSE)
my_result <- if(my_var$my_bool) 1
else my_var$my_bool + 2
return(my_result)
}
The ideal output would be: list(expression(my_var$my_bool), expression(1), expression(my_var$my_bool + 2))
Figured it out! as.list can be used to break up calls into the syntax tree.
For example:
example <- quote(
if (object) {
print(result_true)
} else {
print(result_false)
}
)
as.list(example)
# [[1]]
# `if`
#
# [[2]]
# object
#
# [[3]]
# {
# print(result_true)
# }
#
# [[4]]
# {
# print(result_false)
# }
I make use of the assertthat package quite often to check postconditions in functions. When reading more about the idea of Design by Contract I stumbled upon the idea to make checks of output in comparison to input values.
The most simple example is the following:
toggle <- function(x)!x
One can immediately state that x == !old_x must always be true. (old_x stands for the value of x before evaluation.)
(Of course this example is oversimplified and the postcondition does not add more useful information for humans or computers. A more useful example is on the bottom of the question..)
So I can extend my toggle function as follows to check that condition with every call:
toggle <- function(x){
old_x <- x
x <- !x
assertthat::assert_that(x == !old_x)
return(x)
}
This works of course but I wondered if there's another way to access the value of old_x without explicitely store it (or the result) under a new name. And without splitting the postcondition-checking code to the top and bottom of the function. Something along the line of how R evaluates function calls..
One attempt I can think of is to use sys.call and eval.parent to access to the old values:
toggle <- function(x){
x <- !x
.x <- eval.parent(as.list(sys.call())[[2]])
assertthat::assert_that(x == !.x)
return(x)
}
This works, but I still need to assign a new variable .x and also the subsetting with [[2]] is not flexible yet. However writing it like assertthat::assert_that(x == !eval.parent(as.list(sys.call())[[2]]) does not work and playing around with the search levels of sys.call(-1 ..) did not help.
Another (a bit more useful) example where the postcondition adds some information:
increment_if_smaller_than_2 <- function(x){
old_x <- x
x <- ifelse(x < 2, x <- x + 1, x)
assertthat::assert_that(all(x >= old_x))
return(x)
}
Any hints?
You can access the old parameter-values by accessing it via the parent environment. For this solution to work, you need to introduce new variable(s) for the return-result, i.e. retval, to prevent re-assignments to method-params. IMHO this isn't a serious drawback, since it's good programming-style not to overwrite method-parameters anyway. You could i.e. do the following:
test <- function(.a) {
retval <- 2 * .a
assertthat::assert_that(abs(retval) >= abs(.a))
return(retval)
}
a <- 42
test(a)
# [1] 84
If you would like to take it a step further and submit the assertion-function dynamically you could do that as follows:
test_with_assertion <- function(.a, assertion) {
retval <- 2 * .a
assertthat::assert_that(assertion(retval, eval.parent(.a)))
return(retval)
}
a <- 42
test_with_assertion(a, function(new_value, old_value)
abs(new_value) >= abs(eval.parent(old_value)) )
# [1] 84
Does this do, what you intended to do?
I have the following code in the script test.R:
if (x==2){
stop("the script ends")
}
Now I source this script
source(test.R)
t <- 2
I would like the code to stop if x==2 and does not go further. However, it continues and assigns t <- 2. I can use the function warnings(options) but I want to avoid this option and implement a condition within the if. Any suggestion?
The code you list should work as expected.
As an example, I made two scripts, test.R and test2.R:
1. File test.R:
if (identical(x, 2)) {
stop("the script ends")
}
(Note: I'm using identical(x, 2) as the safer way to check whether x equals 2, but x == 2 would work the same in this example.)
2. File test2.R:
x <- 1
source("test.R")
t <- 1
print("This should be printed.")
x <- 2
source("test.R")
t <- 2
print("This should not be printed!")
Now I run test2.R from the console:
> t <- 5
> source('test2.R')
[1] "This should be printed."
Error in eval(ei, envir) : the script ends
> t
[1] 1
We see that the check passed the first time, when x == 1, and it failed the second time, when x == 2. Therefore, the value of t at the end is 1, because the first assignment was run and the second was not.
A while loop can create a condition that you can escape from as you are suggesting:
while (TRUE){
if (x==2) {
break
}
}
This is assuming that your code is 'all the way to the left' when executing. Seeing a little more might help, or better understanding how x is being set or being used. Note that using something like while(TRUE) might not be best practice, and can lead to infinite execution if you do not exit properly.
I am trying to understand the for and if-statement in r, so I run a code where I am saying that if the sum of rows are bigger than 3 then return 1 else zero:
Here is the code
set.seed(2)
x = rnorm(20)
y = 2*x
a = cbind(x,y)
hold = c()
Now comes the if-statement
for (i in nrow(a)) {
if ([i,1]+ [i,2] > 3) hold[i,] == 1
else ([i,1]+ [i,2]) <- hold[i,] == 0
return (cbind(a,hold)
}
I know that maybe combining for and if may not be ideal, but I just want to understand what is going wrong. Please keep the explanation at a dummy level:) Thanks
You've got some issues. #mnel covered a better way to go about doing this, I'll focus on understanding what went wrong in this attempt (but don't do it this way at all, use a vectorized solution).
Line 1
for (i in nrow(a)) {
a has 20 rows. nrow(a) is 20. Thus your code is equivalent to for (i in 20), which means i will only ever be 20.
Fix:
for (i in 1:nrow(a)) {
Line 2
if ([i,1]+ [i,2] > 3) hold[i,] == 1
[i,1] isn't anything, it's the ith row and first column of... nothing. You need to reference your data: a[i,1]
You initialized hold as a vector, c(), so it only has one dimension, not rows and columns. So we want to assign to hold[i], not hold[i,].
== is used for equality testing. = or <- are for assignment. Right now, if the >3 condition is met, then you check if hold[i,] is equal to 1. (And do nothing with the result).
Fix:
if (a[i,1]+ a[i,2] > 3) hold[i] <- 1
Line 3
else ([i,1]+ [i,2]) <- hold[i,] == 0
As above for assignment vs equality testing. (Here you used an arrow assignment, but put it in the wrong place - as if you're trying to assign to the else)
else happens whenever the if condition isn't met, you don't need to try to repeat the condition
Fix:
else hold[i] <- 0
Fixed code together:
for (i in 1:nrow(a)) {
if (a[i,1] + a[i,2] > 3) hold[i] <- 1
else hold[i] <- 0
}
You aren't using curly braces for your if and else expressions. They are not required for single-line expressions (if something do this one line). They are are required for multi-line (if something do a bunch of stuff), but I think they're a good idea to use. Also, in R, it's good practice to put the else on the same line as a } from the preceding if (inside the for loop or a function it doesn't matter, but otherwise it would, so it's good to get in the habit of always doing it). I would recommend this reformatted code:
for (i in 1:nrow(a)) {
if (a[i, 1] + a[i, 2] > 3) {
hold[i] <- 1
} else {
hold[i] <- 0
}
}
Using ifelse
ifelse() is a vectorized if-else statement in R. It is appropriate when you want to test a vector of conditions and get a result out for each one. In this case you could use it like this:
hold <- ifelse(a[, 1] + a[, 2] > 3, 1, 0)
ifelse will take care of the looping for you. If you want it as a column in your data, assign it directly (no need to initialize first)
a$hold <- ifelse(a[, 1] + a[, 2] > 3, 1, 0)
Such operations in R are nicely vectorised.
You haven't included a reference to the dataset you wish to index with your call to [ (eg a[i,1])
using rowSums
h <- rowSums(a) > 3
I am going to assume that you are new to R and trying to learn about the basic function of the for loop itself. R has fancy functions called "apply" functions that are specifically for doing basic math on each row of a data frame. I am not going to talk about these.
You want to do the following on each row of the array.
Sum the elements of the row.
Test that the sum is greater than 3.
Return a value of 1 or 0 representing the result of 2.
For 1, luckily "sum" is a built in function. It pays off to check out the built in functions within every programming language because they save you time. To sum the elements of a row, just use sum(a[row_number,]).
For 2, you are evaluating a logical statement "is x >3?" where x is the result from 1. The ">3" statement returns a value of true or false. The logical expression is a fancy "if then" statement without the "if then".
> 4>3
[1] TRUE
> 2>3
[1] FALSE
For 3, a true or false value is a data structure called a "logical" value in R. A 1 or 0 value is a data structure called a "numeric" value in R. By converting the "logical" into a "numeric", you can change the TRUE to 1's and FALSE to 0's.
> class(4>3)
[1] "logical"
> as.numeric(4>3)
[1] 1
> class(as.numeric(4>3))
[1] "numeric"
A for loop has a min, a max, a counter, and an executable. The counter starts at the min, and increments until it goes to the max. The executable will run for each run of the counter. You are starting at the first row and going to the last row. Putting all the elements together looks like this.
for (i in 1:nrow(a)){
hold[i] <- as.numeric(sum(a[i,])>3)
}
I am relatively new to R. I am iterating over a vector in R by using for() loop. However, based on a certain condition, I need to skip some values in the vector. The first thought that comes to mind is to change the loop index within the loop. I have tried that but somehow its not changing it. There must be some what to achieve this in R.
Thanks in advance.
Sami
You can change the loop index within a for loop, but it will not affect the execution of the loop; see the Details section of ?"for":
The ‘seq’ in a ‘for’ loop is evaluated at the start of the loop;
changing it subsequently does not affect the loop. If ‘seq’ has
length zero the body of the loop is skipped. Otherwise the
variable ‘var’ is assigned in turn the value of each element of
‘seq’. You can assign to ‘var’ within the body of the loop, but
this will not affect the next iteration. When the loop terminates,
‘var’ remains as a variable containing its latest value.
Use a while loop instead and index it manually:
i <- 1
while(i < 100) {
# do stuff
if(condition) {
i <- i+3
} else {
i <- i+1
}
}
Look at
?"next"
The next command will skip the rest of the current iteration of the loop and begin the next one. That may accomplish what you want.
Without an example it is hard to see what you want to do, but you can always use an if-statement inside a for-loop:
foo <- 1:10*5
for (i in seq(length(foo)))
{
if (foo[i] != 15) print(foo[i])
}
In R, local alterations in the index variable are "corrected" with the next pass:
for (i in 1:10){
if ( i==5 ) {i<-10000; print(i)} else{print(i)}
}
#-----
[1] 1
[1] 2
[1] 3
[1] 4
[1] 10000
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
Since you have some criterion for skipping you should apply the criterion to the loop vector inside the for-parentheses. E.g:
for( i in (1:10)[-c(3,4,6,8,9)] ) {
print(i)}
#----
[1] 1
[1] 2
[1] 5
[1] 7
[1] 10