Is there a built-in function in R for calling binary operators / functions from a list or data frame?
Take, for example, a data frame with three logic elements:
set.seed(10)
foo <- matrix(as.logical(round(runif(24))), ncol = 3)
foo <- as.data.frame(foo)
Now I would like to do something like this:
do.call.bin("|", foo)
so that it applies the or-operator to all columns, giving:
[1] TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
A possible implementation may be:
do.call.bin <- function (fun.bin, lst) {
fun.bin <- match.fun(fun.bin)
if (length(lst) > 2) {
ret <- fun.bin(lst[[1]], Recall(fun.bin, lst[-1]))
} else {
ret <- fun.bin(lst[[1]], lst[[2]])
}
return (ret)
}
However, I doubt that this is not yet implemented in R, although I have not yet found it. Otherwise, a more efficient way to do this?
I can’t use do.call(), since binary operators take only two arguments, and I would like to apply the binary operator to more arguments.
source
share