阅读dplyr中do()
的文档,我对于创建数据组的回归模型的能力印象深刻,并且想知道是否可以使用不同的自变量而非数据组来复制它。
到目前为止,我尝试过:
require(dplyr)
data(mtcars)
models <- data.frame(var = c("cyl", "hp", "wt"))
models <- models %>% do(mod = lm(mpg ~ as.name(var), data = mtcars))
Error in as.vector(x, "symbol") :
cannot coerce type 'closure' to vector of type 'symbol'
models <- models %>% do(mod = lm(substitute(mpg ~ i, as.name(.$var)), data = mtcars))
Error in substitute(mpg ~ i, as.name(.$var)) :
invalid environment specified
希望最终输出的结果像这样:
var slope standard_error_slope
1 cyl -2.87 0.32
2 hp -0.07 0.01
3 wt -5.34 0.56
我知道使用lapply方法可以实现类似的功能,但我觉得apply系列函数很难理解。是否有dplyr的解决方案?
dplyr
参考方面非常准确。 - David Arenburg