0
votes

I have a formula:

form <- outcome ~ var1 + var2 + (!is.na(var3) | var3 == "ANY") + (var1:var2)

Let's say the formula has 4 components:

  • var1
  • var2
  • (!is.na(var3) | var3 == "ANY")
  • (var1:var2)

I would like to obtain a column for each formula component.

In the case of var1 and var2 it would be easy:

dataset$var1
dataset$var2

I know is possible to use eval(parse(text="(!is.na(var3) | var3 == "ANY")")) with a combination of the with function. But it won't work with some formula expressions (like (var1:var2)).

1

1 Answers

0
votes

Have a look at ?terms. In particular, the variables attribute of a terms object is an unevaluated list

attr(terms(form), 'variables')
#list(outcome, var1, var2, !is.na(var3) | var3 == "ANY")

which can be evaluated with eval. Depending on the syntax you want to support in your formula, you may need to write your own terms method. The terms.formula method is the one used by lm and others.