Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a variable from a formula

Tags:

r

Is there a way to extract the response variable from a formula, even if that formula contains a function?

x1 <- ~responsename

as.character(x1[[2]])

x2 <- ~log(responsename)

as.character(x2[[2]][[2]])

Both of these approaches give the desired answer ("responsename"). But is there a way to get the same answer in either case, automatically?

ps: assuming that the formula is always ~ responsename or ~ f(responsename), no + etc.

like image 740
AndrewMacDonald Avatar asked Jan 17 '26 23:01

AndrewMacDonald


1 Answers

Your terminology is non-standard (=wrong!).

The (usually) single "response variable" (aka "dependent variable") is on the left of the tilde ~.

The (often) multiple variables on the right of the tilde are known as "explanatory" or "predictor" or "independent" variables.

Your examples are of one sided formulae with no response variable.

You can use all.vars() to get variable names; @Miff, all.names() returns function names as well.

I guess where there is a response variable it will usually be the first so you could use all.vars(formula)[1], but where (like your examples) it is a one sided formula with no response this will give the first explanatory variable.

Better is to identify the response variable with attr(terms(formula), "response")

f <- y ~ x1 + x2
all.vars(f)[attr(terms(f), "response")]
# [1] "y"
f <- y ~ sin(x1) + cos(x2)
all.vars(f)[attr(terms(f), "response")]
# [1] "y"
f <- ~ x1 + x2
all.vars(f)[attr(terms(f), "response")]
# character(0)
f <- log(y) ~ x1 + x2
all.vars(f)[attr(terms(f), "response")]
# [1] "y"

If you really want the (last specified) explanatory variable then you can use tail(all.vars(formula),1)

like image 151
user20637 Avatar answered Jan 19 '26 20:01

user20637



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!