r - Non standard evaluation (NSE) for dplyr do() -
i implement like
mtcars %>% group_by(cyl) %>% do(mod = lm(mpg ~ disp, data = .))
inside function this
myfun <- function(d, groupvar, x, y) { d %>% group_by(groupvar) %>% do(mod = lm(y ~ x, data = .)) } myfun(mtcars, cyl, disp, mpg)
but cannot understand enough nse it. know, example, dplyr nse functions group_by
or summarize
have associated se functions group_by_
, summarize_
seems do
has not associated do_
.
try
library(dplyr) library(lazyeval) f <- function(d, groupvar, x , y) { groupvar <- lazy(groupvar) x <- lazy(x) y <- lazy(y) d %>% group_by_(groupvar) %>% do(mod = lm(interp(quote(y ~ x), y = y, x = x), data = .)) } f(mtcars, cyl, disp, mpg) # source: local data frame [3 x 2] # groups: <by row> # # cyl mod # 1 4 <s3:lm> # 2 6 <s3:lm> # 3 8 <s3:lm>
Comments
Post a Comment