On 7/19/05, Dirk Enzmann <[EMAIL PROTECTED]> wrote:
> Although I tried to find an answer in the manuals and archives, I cannot
> solve this (please excuse that my English and/or R programming skills
> are not good enough to state my problem more clearly):
> 
> I want to write a function with an indeterminate (not pre-defined)
> number of arguments and think that I should use the "..." construct and
> the match.call() function. The goal is to write a function that (among
> other things) uses cbind() to combine a not pre-defined number of
> vectors specified in the function call. For example, if my vectors are
> x1, x2, x3, ... xn, within the function I want to use cbind(x1, x2) or
> cbind(x1, x3, x5) or ... depending on the vector names I use in the
> funcion call. Additionally, the function has other arguments.
> 
> In the archives I found the following thread (followed by Marc Schwartz)
> 
> http://finzi.psych.upenn.edu/R/Rhelp02a/archive/15186.html
> [R] returning argument names from Peter Dalgaard BSA on 2003-04-10 (stdin)
> 
> that seems to contain the solution to my problem, but I am stuck because
>  sapply(match.call()[-1], deparse) gives me a vector of strings and I
> don't know how to use the names in this vector in the cbind() function.
> 
> Up to now my (clearly deficit) function looks like:
> 
> test <- function(..., mvalid=1)
> {
>   args = sapply(match.call()[-1], deparse)
> # and here, I don't know how the vector names in args
> # can be used in the cbind() function to follow:
> #
> # temp <- cbind( ???
>   if (mvalid > 1)
>   {
> #  here it goes on
>   }
> }
> 
> Ultimately, I want that the function can be called like
> test(x1,x2,mvalid=1)
> or
> test(x1,x3,x5,mavlid=2)
> and that within the function
> cbind(x1,x2)
> or cbind(x1,x3,x5)
> will be used.
> 

If you just need to pass them to cbind then just use cbind(...), e.g.

  test <- function(..., m) if (m > 1) cbind(...) else m

otherwise, use list(...) as shown by a previous answer or here

  test2 <- function(..., m) if (m > 1) length(list(...)) else m

______________________________________________
R-help@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

Reply via email to