简体   繁体   中英

Optimizing for + if in R

I am a bit lost about how to optimize for loops in R. I have a set such that element i belongs to the set iff contains[[i]] == 1 . I want to check whether sets of indices are included in this set. Currently I have the following code. Can it be written more efficiently?

contains = c(1, 0, 0, 1, 0, 1, 1, 0)
indices = c(4, 5) # not ok
# indices = c(4, 6) # ok
ok <- TRUE
for (index in indices) {
    if (contains[[index]] == 0) {
        ok <- FALSE
        break
    }
}
if (ok) {
    print("ok")
} else {
    print("not ok")
}

I would suggest either of these:

ok = all(indices %in% which(contains == 1))
ok = all(contains[indices] == 1)

They will be faster than a for loop in almost all cases. (Exception: if the vectors involved are very long and there is an early discrepancy, your break will stop searching as soon as a first false is found and probably be faster.)

If you need really fast solutions on biggish data, please share some code to simulate data at scale so we can benchmark on a relevant use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM