[R] Defining functions - an interesting problem

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed May 27 16:37:41 CEST 2009


On Wed, 2009-05-27 at 19:41 +0530, utkarshsinghal wrote:
> I define the following function:
> (Please don't wonder about the use of this function, this is just a 
> simplified version of my actual function. And please don't spend your 
> time in finding an alternate way of doing the same as the following does 
> not exactly represent my function. I am only interested in a good 
> explanation)
> 
>  > f1 = function(x,ties.method="average")rank(x,ties.method)
>  > f1(c(1,1,2,4), ties.method="min")
> [1] 1.5 1.5 3.0 4.0
> 
> I don't know why it followed ties.method="average".

What is the second argument of rank? It is not ties.method. You passed
"min" to na.last, not ties.method. You need to name the argument if you
are not passing in all arguments and in the correct order.

> Anyways I randomly tried the following:
> 
>  > f2 = function(x,ties.method="average")rank(x,ties.method=ties.method)
>  > f2(c(1,1,2,4), ties.method="min")
> [1] 1 1 3 4
> Now, it follows the ties.method="min"

Why randomly - ?rank tells you the argument is ties.method so you should
set it to ties.method: times.method = ties.method in your call to rank.

> 
> I don't see any explanation for this, however, I somehow mugged up that 
> if I define it as in "f1", the ties.method in rank function takes its 
> default value which is "average" and if I define as in "f2", it takes 
> the value which is passed in "f2".

Because you aren't passing ties.method as the same argument in f1 and
f2. In f1 you are passing ties.method to na.last, in f2 you do it
correctly.

> 
> But even all my mugging is wasted when I tested the following:
> 
>  > h = function(x, a=1)x^a
>  > g1 = function(x, a=1)h(x,a)
>  > g1(x=5, a=2)
> [1] 25
> 
>  > g2 = function(x, a=1)h(x,a=a)
>  > g2(x=5, a=2)
> [1] 25
> 
> Here in both the cases, "h" is taking the value passed through "g1", and 
> "g2".

Here there are only two arguments and you supplied them in the correct
place when you supplied them un-named.

HTH

G

> 
> Any comments/hints can  be helpful.
> 
> Regards
> Utkarsh
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090527/2526a567/attachment-0002.bin>


More information about the R-help mailing list