arnaud_amsellem@ssga.com
2003-Apr-09 07:55 UTC
[R] How to count the number of parameters in a function
I have the following function: Myfunc <- function(var1,var2,.....,varN) { ..... } In the above function I have a variable number of parameters (N>2). How can I count how many parameters have been entered? Any help appreciated Thanks Arno
Prof Brian Ripley
2003-Apr-09 08:09 UTC
[R] How to count the number of parameters in a function
On Wed, 9 Apr 2003 arnaud_amsellem at ssga.com wrote:> I have the following function: > Myfunc <- function(var1,var2,.....,varN) > { ..... > } > In the above function I have a variable number of parameters (N>2). How can > I count how many parameters have been entered?Well, that example will not parse. If you had Myfunc <- function(...) { dots <- list(...) cat("#args is", length(dots), "\n") } you would be able to see how it might be done. Another way is to use match.call(expand.dots=TRUE), as in Myfunc <- function(...) { Call <- match.call(expand.dots=TRUE) cat("#args is", length(Call) - 1, "\n") } the first element being the function name. In this version you can have named formal arguments and ... . -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Clément Calenge
2003-Apr-09 08:09 UTC
[R] How to count the number of parameters in a function
You can use list(...): Myfunc<-function(...) { d<-list(...) n<-length(d) ## Other instructions return(n) } ## Example: var1<-runif(20) var2<-rnorm(20) var3<-rpois(20, 2) Myfunc(var1, var2, var3) Hope this helps, Clem. At 08:55 09/04/2003 +0100, arnaud_amsellem at ssga.com wrote:>I have the following function: >Myfunc <- function(var1,var2,.....,varN) >{ ..... >} >In the above function I have a variable number of parameters (N>2). How can >I count how many parameters have been entered? > >Any help appreciated > >Thanks > >Arno > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Torsten Hothorn
2003-Apr-09 08:15 UTC
[R] How to count the number of parameters in a function
On Wed, 9 Apr 2003 arnaud_amsellem at ssga.com wrote:> I have the following function: > Myfunc <- function(var1,var2,.....,varN) > { ..... > } > In the above function I have a variable number of parameters (N>2). How can > I count how many parameters have been entered? >using `lm' as example: length(formals(lm)) best, Torsten> Any help appreciated > > Thanks > > Arno > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >