Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove variable from RHS of a formula that has a dot

Tags:

r

formula

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?

like image 897
liori Avatar asked Apr 10 '15 14:04

liori


1 Answers

update(terms(f, data = d), . ~ . - foo)
# cls ~ bar
like image 99
Julius Vainora Avatar answered Nov 08 '22 02:11

Julius Vainora