do: Do anything

Description Usage Arguments Examples

View source: R/deprec-do.r

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("superseded")}

do() is superseded as of dplyr 1.0.0, because its syntax never really felt like it belong with the rest of dplyr. It's replaced by a combination of summarise() (which can now produce multiple rows and multiple columns), nest_by() (which creates a rowwise tibble of nested data), and across() (which allows you to access the data for the "current" group).

Usage

1
do(.data, ...)

Arguments

.data

a tbl

...

Expressions to apply to each group. If named, results will be stored in a new column. If unnamed, should return a data frame. You can use . to refer to the current group. You can not mix named and unnamed arguments.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# do() with unnamed arguments becomes summarise()
# . becomes across()
by_cyl <- mtcars %>% group_by(cyl)
by_cyl %>% do(head(., 2))
# ->
by_cyl %>% summarise(head(across(), 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 %>% summarise(broom::tidy(mod))

javifar/TIDYVERSE-DPLYR documentation built on Dec. 20, 2021, 9:08 p.m.