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: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Andreas Leha > Sent: Friday, November 01, 2013 2:50 PM > To: r-h...@stat.math.ethz.ch > Subject: Re: [R] quickly extract response from formula > > Hi David, > > thanks for your quick answer! > > David Winsemius <dwinsem...@comcast.net> 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 > > ______________________________________________ > R-help@r-project.org 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. ______________________________________________ R-help@r-project.org 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.