Home > front end >  How can I check in R if a function is in a list of functions?
How can I check in R if a function is in a list of functions?

Time:05-13

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?

CodePudding user response:

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

CodePudding user response:

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

  •  Tags:  
  • r
  • Related