R: Passing A Formula To lm

Kevin Feasel



John Mount has a new R tip, this time around passing a formula and seeing that formula later:

This works, and the paste() pattern is so useful we suggest researching and memorizing it.

However the “call” portion of the model is reported as “formula = f” (the name of the variable carrying the formula) instead of something more detailed. Frankly this printing issue never bothered us. None of our tools or workflows currently use the model call item, and for a very large number of variables formatting the call contents in the model report becomes unweildy. We also already have the formula in a variable, so if we need it we can save it or pass it along.

There is a much better place on many models to get model structure information from than the model call item: the model terms item. This item carries a lot of information and formats up quite nicely:

# [1] "mpg ~ cyl + disp + hp + carb"

Be sure to check out the comments too, as there are several solutions to this problem.

Related Posts

Donating To The R Foundation

Mark Niemann-Ross explains how you can donate to the R Foundation: I benefit from the work of the R Foundation. They oversee the language, but also encourage a healthy ecosystem. CRAN happens because of them. Updates to R happen because of them. useR! happens because of them. Every day, you and I are the recipients […]

Read More

Timing Means Of Groups With R

John Mount shares some performance measures pitting data.table against various dplyr methods for calculating grouped means: In this reproduction attempt we see:– The dplyr time being around 0.05 seconds. This is about 5 times slower than claimed.– The dplyr sum()/n() time is about 0.2 seconds, about 5 times faster than claimed.– The data.table time being around 0.004 seconds. This is about three times as […]

Read More


September 2018
« Aug Oct »