Hi everyone,
This is a bit basic, but I'd like to learn the best practice. I have a very long chunk of code, and sometimes it needs to exist within a for
loop, depending on whether a certain condition is met (for example, setting a variable my_answer
to "YES"
. )
If my_answer
is not "YES"
, then I want to execute the code, but not within the loop.
My current understanding is to paste that exact same (long) block of code after else
since the initial condition is not met....but this doubles the size of my code. Is there some better way of doing this than the way I'm describing:
my_answer <- c("YES")
if(my_answer=="YES"){
for(i in 1:1000){
###very long code####
###it will be looped over 1000 times####
}
} else {
###very long code####
#Not looped###
}
Thanks!