I am trying to create generic function to handle a data frame with multiple plausible values. What I want is to pass a formula to a function to perform a regression such as:
f <- MRPCM ~ DSEX + IEP + ELL3 + SDRACEM + PARED
The MRPCM
variable does not actually exist in the data frame. Instead five variables, MRPCM1
, MRPCM2
, MRPCM3
, MRPCM4
, and MRPCM5
do exist. What I want to do is iterate and update the formula (f
here) to create five formulas. Can this be done? The update.formula
function seems to work on the entire left or right side at a time. I should also note that in this example the variable I wish to change is the dependent variable so that update(f, MRPCM1 ~ .)
works. However, I will not know where the variable appears in the formula.
For example:
f <- MRPCM + DSEX ~ IEP + ELL3 + SDRACEM + PARED
update.formula(f, as.formula('MRPCM1 ~ .'))
Results in this (note that DSEX is missing now):
MRPCM1 ~ IEP + ELL3 + SDRACEM + PARED