[R] optional fields in function declarations; Solved

claudio.is at libero.it claudio.is at libero.it
Sun May 20 11:00:37 CEST 2007


thank you to both Adaikalavan and Patrick.


on the basis of Adaikalavan example this is an example that point out  my problems.

>log_raise=function(num, exp, base){return(log(num^exp,base))}

I would like to have optional fields, so some settings to be default parameters;
if I declare the function as above the operator must of course input all the variables.

Finally I could fix as follow.

> log_raise=function(num, exp=2, base=exp){return(log(num^exp,base))}

thank you helps, cheers



Claudio


Patrick Burns

---------- Initial Header -----------

>From      : "Adaikalavan Ramasamy" ramasamy at cancer.org.uk
To          : "claudio.is at libero.it" claudio.is at libero.it
Cc          : "r-help" r-help at stat.math.ethz.ch
Date      : Sun, 20 May 2007 00:21:32 +0100
Subject : Re: [R] optional fields in function declarations







> Can you provide an simple example of what you want the function to do?
> 
> Generally, I set some value in the default.
> 
> raise <- function(x, power=1){ return( x^power ) }
> 
>  > raise(5)
> [1] 5
>  > raise(5,3)
> [1] 125
> 
> 
> Or you can do the same but in a slightly unclear manner.
> 
> raise <- function(x, power){
>    if(missing(power)) power <- 1
>    return( x^power )
> }
> 
> I prefer the former.
> 
> Regards, Adai
> 
> 
> 
> claudio.is at libero.it wrote:
> > Dear R users,
> > 
> > I need to create a set of function to solve some tasks. I want to leave the operator to decide whether uses default parameters or change it; so the functions may have some optional fields. I tied to use the function missing(), but it will work properly only if the optional field is decleared at last in the function.
> > Can you give me some suggestion an some reference?
> > 
> > thank you.
> > 
> > 
> > Claudio
> > 
> > 
> > ------------------------------------------------------
> > Passa a Infostrada. ADSL e Telefono senza limiti e senza canone Telecom
> > http://click.libero.it/infostrada
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> > 
> > 
> > 
> 
> 


------------------------------------------------------
Leggi GRATIS le tue mail con il telefonino i-mode™ di Wind
http://i-mode.wind.it/



More information about the R-help mailing list