I often like to fit and examine multiple models that relate two variables in an R dataframe.
I can do that using syntax like this:
require(tidyverse)
require(broom)
models <- list(hp ~ exp(cyl), hp ~ cyl)
map_df(models, ~tidy(lm(data=mtcars, formula=.x)))
But I'm used to the pipe syntax and was hoping to be able to something like this:
mtcars %>% map_df(models, ~tidy(lm(data=., formula=.x)))
That makes it clear that I'm "starting" with mtcars
and then doing stuff to it to generate my output. But that syntax doesn't work, giving an error Error: Index 1 must have length 1
.
Is there a way to write my purrr:map()
function in a way that I can pipe mtcars
into it to get the same output as the working code above? I.e.
mtcars %>% <<<something>>>