| do | R Documentation |
do() is superseded as of dplyr 1.0.0, because its syntax never really
felt like it belonged with the rest of dplyr. It's replaced by a combination
of reframe() (which can produce multiple rows and multiple columns),
nest_by() (which creates a rowwise tibble of nested data),
and pick() (which allows you to access the data for the "current" group).
do(.data, ...)
.data |
a tbl |
... |
Expressions to apply to each group. If named, results will be
stored in a new column. If unnamed, must return a data frame. You can
use |
# do() with unnamed arguments becomes reframe() or summarise()
# . becomes pick()
by_cyl <- mtcars |> group_by(cyl)
by_cyl |> do(head(., 2))
# ->
by_cyl |> reframe(head(pick(everything()), 2))
by_cyl |> slice_head(n = 2)
# Can refer to variables directly
by_cyl |> do(mean = mean(.$vs))
# ->
by_cyl |> summarise(mean = mean(vs))
# do() with named arguments becomes nest_by() + mutate() & list()
models <- by_cyl |> do(mod = lm(mpg ~ disp, data = .))
# ->
models <- mtcars |>
nest_by(cyl) |>
mutate(mod = list(lm(mpg ~ disp, data = data)))
models |> summarise(rsq = summary(mod)$r.squared)
# use broom to turn models into data
models |> do(data.frame(
var = names(coef(.$mod)),
coef(summary(.$mod)))
)
# ->
models |> reframe(broom::tidy(mod))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.