简体   繁体   中英

How can I check in R if a function is in a list of functions?

algos <- c(bnlearn::hc, bnlearn::tabu)
bnlearn::hc %in% algos

Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments

I've tried the above with the following error. How can I resolve this?

You can check whether functions are equal by using the identical function.

To check for list membership, you will manually need to apply it to the list:

any(vapply(algos, identical, logical(1L), bnlearn::hc))

And you can of course wrap that into a function/operator:

`%fin%` = function (fun, funs) {
    any(vapply(funs, identical, logical(1L), match.fun(fun)))
}
bnlearn::hc %fin% algos

If you build your list as a named list in the first place:

algos <- c("bnlearn::hc" = bnlearn::hc, "bnlearn::tabu" = bnlearn::tabu)

You could simply do:

"bnlearn::hc" %in% names(algos)

TRUE

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