[R] How to parse the arguments from a function call and evaluate them in a dataframe?

Ravi Varadhan rvaradhan at jhmi.edu
Sat Mar 6 00:22:19 CET 2010


Hi,

I would like to write a function which has the following syntax:

myfn <- function(formula, ftime, fstatus, data) {
# step 1: obtain terms in `formula' from dataframe `data'
# step 2: obtain ftime from `data'
# step 3:  obtain fstatus from `data'
# step 4: do model estimation
# step 5: return results
}

The user would call this function as:

myfn(formula=myform, ftime=myftime, fstatus=myfstatus, data=mydata)

Where `myform' is a formula object; and the terms in `myform', and the variables `myftime' and `myfstatus' should be obtained from the dataframe `mydata'.

I am getting tripped up in trying to figure out how to do the seemingly simple steps of 1, 2, and 3.  

I looked at the code for `lm', `coxph', `nls' etc, but they are too complicated for my understanding.  Is there a simple way to accomplish this?  

Thanks very much,
Ravi.
____________________________________________________________________

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu



More information about the R-help mailing list