You can bullet-proof it a bit by making sure that length(formula)==3 before assuming that formula[[2]] is the response. If length(formula)==2 then there is no response term, only predictor terms. E.g., replace resp <- frm[[2]] with resp <- if (length(frm)==3) frm[[2]] else NULL (or call stop(), or warning(), ...)
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [email protected] [mailto:[email protected]] On > Behalf > Of Andreas Leha > Sent: Friday, November 01, 2013 2:50 PM > To: [email protected] > Subject: Re: [R] quickly extract response from formula > > Hi David, > > thanks for your quick answer! > > David Winsemius <[email protected]> writes: > > > On Oct 31, 2013, at 1:27 PM, Andreas Leha wrote: > > > >> Hi all, > >> > >> what is the recommended way to quickly (and without much burden on the > >> memory) extract the response from a formula? > > > > If you want its expression value its just form[[2]] > > > > If you wnat it evaluated in the environment of a dataframe then this should > > be fairly > efficient: > > > > x <- stats::runif(20) > > y <- stats::runif(20) > > dfrm <- data.frame(x=x,y=y) > > extractResponse <- function(frm, dat) { resp <- frm[[2]]; print(resp) # > > that's optional > > fdat <- eval(resp, > > envir=dat); return(fdat) } > > This is what I'll be using. Thanks again! > > [...] > > Regards, > Andreas > > ______________________________________________ > [email protected] 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. ______________________________________________ [email protected] 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.

