I have a function with an structure like this:
function(x, y, z, n = 3){
test = #funky fit with the input data
other = #more metrics
#conditions to evaluate the model
if (condition1 = TRUE){ #condition based on other or test
#rerun function with n = 2
} else if (condition2 = TRUE) {#condition based on other or test
#rerun function with n = 1
} else {
#save the output
}
}
I'm wondering how can I re run the function when condition 1 or 2 are met changing n
to 2 or 1 respectively. I am aware of while
but I'm not sure how to implement it on this situation.
CodePudding user response:
Why not just use recursion?
recurse = function(x, y, z, n = 3){
test = #funky fit with the input data
other = #more metrics
if (condition1){
recurse(x, y, z, n = 2)
} else if (condition2) {
recurse(x, y, z, n = 1)
} else {
# Choose the return value here
other
}
}