Re: [R] Pass an operator to function

2010-12-01 Thread Duncan Murdoch
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 fun

Re: [R] Pass an operator to function

2010-11-30 Thread sachinthaka . abeywardana
If you only want to deal with the less than or greater than operation a cheap trick would be: test <- function(a, b, sign) { foo <- (a*sign > b*sign); return(foo); } might have to tweak the syntax. The idea behind this is that 5>3: TRUE -5>-3: FALSE (i.e. I've multiplied both sides by sign(-

Re: [R] Pass an operator to function

2010-11-30 Thread randomcz
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. __ R-help@r-project.org mailing list https://stat.ethz.ch/m

Re: [R] Pass an operator to function

2010-11-30 Thread Michael Bedward
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 wrote: > > Hi guys, > > How to pass an operator to a function. For examp

Re: [R] Pass an operator to function

2010-11-30 Thread David Winsemius
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

Re: [R] Pass an operator to function

2010-11-30 Thread Joshua Wiley
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(

[R] Pass an operator to function

2010-11-30 Thread randomcz
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 he