I have a formula from which I want to deduce the name of the response variable. One way of doing so is as follows:> my.form <- as.formula("y ~ x + z") > all.vars(my.form)[1][1] "y">Is there a better way and/or preferrred method of determining "y" from my.form than this one? In messing around with terms, I came up with:> all.vars(terms(my.form))[attr(terms(my.form), "response")][1] "y">But that seems too ugly to be best. Thanks, Dave Kane> R.version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 1.0 year 2005 month 04 day 18 language R>
David Kane wrote:> I have a formula from which I want to deduce the name of the response > variable. One way of doing so is as follows: > > >>my.form <- as.formula("y ~ x + z") >>all.vars(my.form)[1] > > [1] "y" > > > Is there a better way and/or preferrred method of determining "y" from > my.form than this one? In messing around with terms, I came up with: > > >>all.vars(terms(my.form))[attr(terms(my.form), "response")] > > [1] "y" > > > But that seems too ugly to be best. > > Thanks, > > Dave Kane > > >>R.version > > _ > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 2 > minor 1.0 > year 2005 > month 04 > day 18 > language R > >David, Using all.vars as you have might be dangerous if you ever encounter a one sided formula. For example, all.vars(y ~ x)[1] # "y" all.vars(~x)[1] # "x" You might want to look at nlme::getResponseFormula or I wrote a function a while back: parse.formula <- function(formula) { vars <- terms(as.formula(formula)) y <- if(attr(vars, "response")) nlme::getResponseFormula(formula) x <- nlme::getCovariateFormula(formula) z <- nlme::getGroupsFormula(formula) list(response = all.vars(y), covariates = all.vars(x), groups = all.vars(z)) } parse.formula(y ~ x)$response # "y" parse.formula( ~ x)$response # character(0) HTH, --sundar
is it what you want?>dat<-data.frame(x=rnorm(10),y=rnorm(10),z=rnorm(10)) > my.form <- as.formula(y ~ x + z) > my.formy ~ x + z> m<-model.frame(my.form,data=dat) > model.extract(m,"response")1 2 3 4 5 6 7 -0.3434826 1.0145622 -0.4749584 0.4018080 -0.3039126 -0.8180650 0.5455521 8 9 10 1.1460328 0.8038568 1.1092655 ======= 2005-07-12 22:43:33 伳侜佋佢伬伌佇伵佒佇佇伌伒伬仯伜======>I have a formula from which I want to deduce the name of the response >variable. One way of doing so is as follows: > >> my.form <- as.formula("y ~ x + z") >> all.vars(my.form)[1] >[1] "y" >> > >Is there a better way and/or preferrred method of determining "y" from >my.form than this one? In messing around with terms, I came up with: > >> all.vars(terms(my.form))[attr(terms(my.form), "response")] >[1] "y" >> > >But that seems too ugly to be best. > >Thanks, > >Dave Kane > >> R.version > _ >platform i686-pc-linux-gnu >arch i686 >os linux-gnu >system i686, linux-gnu >status >major 2 >minor 1.0 >year 2005 >month 04 >day 18 >language R >> > >______________________________________________ >R-help at 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= = = = = = = = = = = = = = = = = = = 2005-07-12 ------ Deparment of Sociology Fudan University Blog:http://sociology.yculblog.com