Hi guys,
How to pass an operator to a function. For example,
test <- function(a, ">", b)
{
return(a>b) #the operator is passed as an argument
}
Thanks,
--
View this message in context:
http://r.789695.n4.nabble.com/Pass-an-operator-to-function-tp3066627p3066627.html
Sent from the R help mailing list archive at Nabble.com.
Here's one way that works with *some* operators (I do not believe you
actually could generalize to *every* operator because some are unary
and some are binary (see example cases).
test <- function(a, op, b) {
foo <- match.fun(FUN = op)
return(foo(a, b))
}
test(5, ">", 4)
test(5, "<", 4)
test(5, "+", 4)
test(5, "!", 4) # fails because unary
test(5, "-", 4)
test(5, "*", 4)
HTH,
Josh
On Tue, Nov 30, 2010 at 6:54 PM, randomcz <randomcz at gmail.com>
wrote:>
> Hi guys,
>
> How to pass an operator to a function. For example,
>
> test <- function(a, ">", b)
> {
> ? ? ?return(a>b) #the operator is passed as an argument
> }
>
> Thanks,
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Pass-an-operator-to-function-tp3066627p3066627.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
On Nov 30, 2010, at 9:54 PM, randomcz wrote:> > Hi guys, > > How to pass an operator to a function. For example, > > test <- function(a, ">", b) > { > return(a>b) #the operator is passed as an argument > }I think you have just requested the definition of do.call() although you infix positioning is a bit non-standard: ?do.call > do_this <- function(a, fn=">", b) {do.call(fn, list(a , b))} > do_this(a=1, b=4) [1] FALSE > do_this(a=1, b=0) [1] TRUE>-- David Winsemius, MD West Hartford, CT
Here is one way...
f <- function(a, b, op="==") {
?call <- call(op, a, b)
?result <- eval(call)
?# possibly do other stuff
?result
}
> f(1, 2)
[1] FALSE> f(1, 2, "<")
[1] TRUE
Michael
On 1 December 2010 13:54, randomcz <randomcz at gmail.com>
wrote:>
> Hi guys,
>
> How to pass an operator to a function. For example,
>
> test <- function(a, ">", b)
> {
> ? ? ?return(a>b) #the operator is passed as an argument
> }
>
> Thanks,
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Pass-an-operator-to-function-tp3066627p3066627.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Thanks, that helps. -- View this message in context: http://r.789695.n4.nabble.com/Pass-an-operator-to-function-tp3066627p3066696.html Sent from the R help mailing list archive at Nabble.com.
On 30/11/2010 9:54 PM, randomcz wrote:> > Hi guys, > > How to pass an operator to a function. For example, > > test<- function(a, ">", b) > { > return(a>b) #the operator is passed as an argument > } > > Thanks, >It's much simpler than the other suggestions. Just pass the operator, and treat it as a function. (Most examples assume you're passing the name of the operator and have to retrieve the function. No need for that.) The only complication is that operator names are not generally seen simply as identifiers by the parser, so they need backquotes when you refer to them. So the function can simply be test <- function(a, op, b) { op(a,b) } and then you put the operator in back quotes to pass it: > test(4, `<`, 5) [1] TRUE > test(4, `>`, 5) [1] FALSE Duncan Murdoch