[R] argument rationalization

Robin Hankin rksh at soc.soton.ac.uk
Mon Nov 15 09:44:12 CET 2004


Hi

I am writing a bunch of functions that take two, three or four
arguments.  These functions operate on vectors of the same length; 
but  I want the function
to behave sensibly if one or more arguments are scalars.  "+" does 
this for two arguments:


"+"(1:10,3)    # interpreted as "+"(1:10,rep(3,10))

But my functions can take more arguments.  Say f() takes three:

f(1:10,1:10,1:10)  # default
f(3,1:10,1:10)     # interpret as f(rep(3,10),1:10,1:10)
f(1:10,3,1:10)     # interpret as f(1:10,rep(3,10),1:10)
f(1:10,3,5)        # interpret as f(1:10,rep(3,10),rep(5,10))

and h() takes four:

h(2,4,5,1:10)    # interpret as h(rep(2,10),rep(4,10),rep(5,10),1:10)
h(2,3,1:10,1)    # interpret as h(rep(2,10),rep(3,10),1:10,rep(1:10)
h(1:20,3,1:20,1) # interpret as h(1:20,rep(3,20),1:20,rep(1,20))

I haven't got any that need five yet, but this may change in the future.
How do I implement this desired behaviour nicely?

(I pass the arguments to .C(), which is why I need this).

-- 
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
SO14 3ZH
tel +44(0)23-8059-7743
initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)




More information about the R-help mailing list