I have a data frame and a formula stored in variables:
> d <- data.frame(cls=1, foo=2, bar=3)
> f <- formula(cls ~ .)
I'd like to remove one variable from the RHS of this formula programatically (in my code, the name of this variable would be passed somewhere as a string). I tried using update.formula
:
> update(f, .~.-foo)
Error in terms.formula(tmp, simplify = TRUE) :
'.' in formula and no 'data' argument
Then I tried providing the data
argument:
> update(f, .~.-foo, data=d)
Error in terms.formula(tmp, simplify = TRUE) :
'.' in formula and no 'data' argument
I know the above would work if the initial formula didn't have a dot on the right side:
> f <- formula(cls ~ foo + bar)
> update(f, .~.-foo)
cls ~ bar
How do I remove a variable from RHS of a formula if I can't ensure that RHS doesn't contain a dot?
update(terms(f, data = d), . ~ . - foo)
# cls ~ bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With