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. use
update(my_formula, ~.-e)
# a ~ b + (1 | d)
The -e
means drop the "e" term from the formula while .
means keep everything else.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.