rr-formula

Create formula using the name of a data frame column


Given a data.frame, I would like to (dynamically) create a formula y ~ ., where y is the name of the first column of the data.frame.

What complicates this beyond the approach of as.formula(paste(names(df)[1], "~ .")) is that the name of the column might be a function, e.g.:

names(model.frame(lm(I(Sepal.Length/Sepal.Width) ~ Species, data = iris)))[1] is "I(Sepal.Length/Sepal.Width)"

So I need the column name to be quoted, i.e. in the above example I would want the formula to be `I(Sepal.Length/Sepal.Width)` ~ ..

This works:

df <- model.frame(lm(I(Sepal.Length/Sepal.Width) ~ Species, data = iris))
fm <- . ~ .
fm[[2]] <- as.name(names(df)[1])

But is there a neat way to do it in one step?


Solution

  • We could use reformulate

    reformulate(".", response = sprintf("`%s`", names(df)[1]))