I have a formula:
my_formula <- a ~ b + (1|d) + e
I want to programmatically remove the e
variable from this formula.
What I've done for fixed-effects only formulae is:
drop_es <- function(f) {
e_idxs <- grep(pattern = 'e',
x = all.vars(f[[3]]))
stats::formula(stats::drop.terms(termobj = stats::terms(f),
dropx = e_idxs,
keep.response = TRUE))
}
This works on fixed-effects only formula:
drop_es(f = a ~ b + e)
a ~ b
But on a formula with a random effect, it drops the parentheses:
drop_es(f = a ~ b + (1|d) + e)
a ~ b + 1 | d
This is a really important distinction -- without the parentheses, it is interpreted as:
a ~ b|d
How can I remove the e
terms without losing the parentheses?
The
update
function exists to modify formulas. useThe
-e
means drop the "e" term from the formula while.
means keep everything else.