How to execute a statement only if a condition is met in R-exams

297 Views Asked by At

I intend the code to run only if c is different from a square less than 36, otherwise try again ...

c<-sample((1:36),1)
if(c==1|c==4|c==9|c==16|c==25|c==36){

}else{
  
}
2

There are 2 best solutions below

2
On BEST ANSWER

Maybe a simplier condition:

if (c %in% (1:5)^2) {...}
2
On

The following will work:

gotit <- FALSE

while (!gotit)
{
  c<-sample((1:36),1)
  if(c==1|c==4|c==9|c==16|c==25|c==36){
    gotit <- FALSE
  } else {
    gotit <- TRUE
    cat("Got it!\n")
  }
}