The lattice package uses special logic to allow for multiple left-hand-side variables in a formula, e.g. y1 + y2 ~ x. Is there an elegant way to do this outside of lattice? I'm trying to implement a data summarization function that logically takes multiple dependent variables. The usual invocation of model.frame( ) causes R to try to do arithmetic addition to create a single dependent variable. Thanks Frank ----- Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: http://r.789695.n4.nabble.com/Multiple-left-hand-side-variables-in-a-formula-tp4660060.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2013-Mar-02 00:50 UTC
[R] Multiple left hand side variables in a formula
On Fri, Mar 1, 2013 at 7:16 PM, Frank Harrell <f.harrell at vanderbilt.edu> wrote:> The lattice package uses special logic to allow for multiple left-hand-side > variables in a formula, e.g. y1 + y2 ~ x. Is there an elegant way to do > this outside of lattice? I'm trying to implement a data summarization > function that logically takes multiple dependent variables. The usual > invocation of model.frame( ) causes R to try to do arithmetic addition to > create a single dependent variable. >Try: lm( cbind(Sepal.Length, Sepal.Width) ~., iris) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
I don't know how much of the information that model.frame supplies you need, but you could make a data.frame containing all the variables on both sides of them formula by changing lhs~rhs into ~lhs+rsh before calling model.frame. E.g., f <- function (formula) { if (length(formula) == 3) { # has left hand side envir <- environment(formula) formula <- formula(call("~", call("+", formula[[2]], formula[[3]]))) environment(formula) <- envir } formula } This doesn't quite take care of the wild-card dot in the formula: straight variables are omitted from dot's expansion but functions of variables are not:> colnames(model.frame(f(log(mpg)+hp ~ .), data=mtcars))[1] "log(mpg)" "hp" "mpg" "cyl" [5] "disp" "drat" "wt" "qsec" [9] "vs" "am" "gear" "carb" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Frank Harrell > Sent: Friday, March 01, 2013 4:17 PM > To: r-help at r-project.org > Subject: [R] Multiple left hand side variables in a formula > > The lattice package uses special logic to allow for multiple left-hand-side > variables in a formula, e.g. y1 + y2 ~ x. Is there an elegant way to do > this outside of lattice? I'm trying to implement a data summarization > function that logically takes multiple dependent variables. The usual > invocation of model.frame( ) causes R to try to do arithmetic addition to > create a single dependent variable. > > Thanks > Frank > > > > ----- > Frank Harrell > Department of Biostatistics, Vanderbilt University > -- > View this message in context: http://r.789695.n4.nabble.com/Multiple-left-hand-side- > variables-in-a-formula-tp4660060.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
Possibly Parallel Threads
- Bug in by() function which works for some FUN argument and does not work for others
- Does a formula object have a "left hand side"
- Bug in by() function which works for some FUN argument and does not work for others
- Bug in by() function which works for some FUN argument and does not work for others
- Resultado de la consola como un tibble