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(ab) #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
Hi guys,
How to pass an operator to a function. For example,
test - function(a, , b)
{
return(ab) #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
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, +,
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(ab) #the operator is passed as an argument
}
I think you have just requested the definition of do.call() although
you infix positioning is a
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 rando...@gmail.com wrote:
Hi guys,
How to pass an operator to a function. For
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
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
53: TRUE
-5-3: FALSE (i.e. I've multiplied both sides by sign(-1))
7 matches
Mail list logo