r - How to exit loop when there's an error -


i have loop in search value in matrix. when no such value exits, function throw error. want exit loop when error occurs. how do this?

i'm thinking this, not sure how execute in r.

for (i in 1:n){ val<-#find value in matrix if (val returns error) break } 

thanks!

you can indeed do:

vec = c(1,2,3,5,6)  for(u in 1:10){      if(!is.element(u, vec))      {          print(sprintf("element %s not found in vec", u))           break      }      print(sprintf("element %s found in vec", u)) }  #[1] "element 1 found in vec" #[1] "element 2 found in vec" #[1] "element 3 found in vec" #[1] "element 4 not found in vec" 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -