如何仅在 R-exams 中满足条件时才执行语句

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

仅当 c 不同于小于 36 的正方形时,我才打算将代码设为 运行,否则重试 ...

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

}else{
  
}

以下将起作用:

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")
  }
}

也许更简单的条件:

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