f_nest_by | R Documentation |
A faster nest_by()
.
f_nest_by(
data,
...,
.add = FALSE,
.order = group_by_order_default(data),
.by = NULL,
.cols = NULL,
.drop = df_group_by_drop_default(data)
)
data |
data frame. |
... |
Variables to group by. |
.add |
Should groups be added to existing groups?
Default is |
.order |
Should groups be ordered? If |
.by |
(Optional). A selection of columns to group by for this operation.
Columns are specified using |
.cols |
(Optional) alternative to |
.drop |
Should unused factor levels be dropped? Default is |
A row-wise grouped_df
of the corresponding data of each group.
library(dplyr)
library(fastplyr)
# Stratified linear-model example
models <- iris |>
f_nest_by(Species) |>
mutate(model = list(lm(Sepal.Length ~ Petal.Width + Petal.Length, data = first(data))),
summary = list(summary(first(model))),
r_sq = first(summary)$r.squared)
models
models$summary
# dplyr's `nest_by()` is admittedly more convenient
# as it performs a double bracket subset `[[` on list elements for you
# which we have emulated by using `first()`
# `f_nest_by()` is faster when many groups are involved
models <- iris |>
nest_by(Species) |>
mutate(model = list(lm(Sepal.Length ~ Petal.Width + Petal.Length, data = data)),
summary = list(summary(model)),
r_sq = summary$r.squared)
models$summary
models$summary[[1]]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.