I'm using model.frame()
in R (to use within a function) but I can't manage to exclude some of the variables with the formula by using -
.
For example, in a data frame (data) with variables "y", "x1", "x2" and "x3", by using:
model.frame(y ~ . - x3, data)
I would get a data frame including all "y", "x1", "x2" and "x3".
Is there a way to exclude "x3" using a formula and not removing the variable directly as in:data[,-4]
?
I can't figure out how to do this super cleanly, but you can do it in a couple of steps:
# example data
data <- data.frame(y=0,x1=1,x2=2,x3=3)
Get the full expanded formula in the context of data
and then remove x3
:
mf <- model.frame(y ~ ., data, subset=FALSE)
#formula(mf)
##y ~ x1 + x2 + x3
model.frame(update(formula(mf), ~ . -x3), data=data)