I have a for loop in R. Inside the for loop I have
kl<-stri_detect_fixed(nn, "new")
In some cases, kl is logical(0). If this is the case I want to skip the current iteration and move on to the next in R.
I tried something like
if (is.logical(kl)==T) {
next
}
but is does not work. Any ideas?
Many thanks
Return value of stringi::stri_detect_fixed()
is a logical vector, so is.logical(kl)
is always TRUE
.
But you can test if its length is 0:
lst <- list(a = c("foo", "bar"),
b = NULL,
c = "news")
for (nn in lst){
kl <- stringi::stri_detect_fixed(nn, "new")
if (length(kl) < 1) next
message("no skip for ", paste(nn, collapse = ", "))
}
#> no skip for foo, bar
#> no skip for news
Or if input is NULL
:
for (nn in lst){
if (is.null(nn)) next
kl <- stringi::stri_detect_fixed(nn, "new")
message("no skip for ", paste(nn, collapse = ", "))
}
#> no skip for foo, bar
#> no skip for news
Created on 2024-09-21 with reprex v2.1.1