我想从函数中返回值,并在返回值的行上结束函数。
myfunc <- function(){
if(TRUE){
return(1) #end function here and do not execute the rest of the code.
}
if(FALSE){
return(2)
}
return(3)
}
但是当执行这个函数时,它返回3
。如果第一个条件为真,我如何才能只返回值?
发布于 2020-02-06 18:50:55
myfunc <- function(condition){
if(condition) return(1)
else return(2)
}
myfunc <- function(condition){
if(condition) return(1)
2 # the last call in a function is returned
}
myfunc()
[1] 1
https://stackoverflow.com/questions/60093196
复制相似问题