[R] using argument names (of indeterminate number) within a function

From: Dirk Enzmann <dirk.enzmann_at_jura.uni-hamburg.de>
Date: Wed 20 Jul 2005 - 01:29:25 EST


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.

Can someone give and explain an example / a solution on how to proceed?



Dr. Dirk Enzmann
Institute of Criminal Sciences
Dept. of Criminology
Edmund-Siemers-Allee 1
D-20146 Hamburg
Germany

phone: +49-040-42838.7498 (office)

        +49-040-42838.4591 (Billon)
fax: +49-040-42838.2344
email: dirk.enzmann@jura.uni-hamburg.de
www:
http://www2.jura.uni-hamburg.de/instkrim/kriminologie/Mitarbeiter/Enzmann/Enzmann.html



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 Received on Wed Jul 20 01:33:12 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:48 EST