I have a formula that looks as follows:

```
formula <- as.formula(y ~ x + as.factor(z) + A + as.factor(B) + C:as.factor(A) + as.factor(D) + E + F + as.factor(G))

```

I would like to extract all the variable names that have factors to turn them to factors. If I use `all.vars(formula)`, I get all variables and not just the `as.factor()`.

Desired result:

```
factornames <- c("z", "B", "A", "D", "G")

```

I eventually want to feed the selected variables to:

```
# Turn factors into factors
DF[factornames] <- lapply(DF[factornames], factor)
## turn factor variables into dummies
DF <- as.data.frame(model.matrix(phantom ~ ., transform(DF, phantom=0)))

```