tl/dr: mtcars %>% {map_df(models, function(.x) tidy(lm(data=., formula=.x)))}
Or mtcars %>% map_df(models, ~tidy(lm(..1,..2)), ..2 = .)
您尝试过的解决方案存在两个问题。
首先,如果您想将点放置在不寻常的位置,则需要使用花括号。
library(magrittr)
1 %>% divide_by(2) # 0.5 -> this works
1 %>% divide_by(2,.) # 2 -> this works as well
1 %>% divide_by(2,mean(.,3)) # this doesn't
1 %>% divide_by(.,2,mean(.,3)) # as it's equivalent to this one
1 %>% {divide_by(2,mean(.,3))} # but this one works as it forces all dots to be explicit.
第二个是你不能将点与~
按照您想要的方式配制,尝试map(c(1,2), ~ 3+.)
and map(c(1,2), ~ 3+.x)
(甚至map(c(1,2), ~ 3+..1)
),你会看到得到相同的结果。当你在 a 中使用点时~
公式它不再链接到管道函数。
确保该点被解释为mtcars
你需要使用旧的function(x) ...
定义。
这有效:
mtcars %>% {map_df(models, function(.x) tidy(lm(data=., formula=.x)))}
最后,作为奖励,这就是我想出的,试图找到一个没有大括号的解决方案:
mtcars %>% map(models,lm,.) %>% map_df(tidy)
mtcars %>% map_df(models, ~tidy(lm(..1,..2)), ..2 = .)