Closed
Description
With the idea of (finally!) coming up with a formal replacement.
Imaginary code:
gapminder %>%
nest_by(country) %>%
mutate(fit = lm(lifeExp ~ year, data = data)) %>%
summarise(broom::tidy(fit))
Equivalent to:
library(gapminder)
library(tidyverse)
gapminder %>%
group_by(country) %>%
nest() %>%
mutate(
fit = map(data, ~ lm(lifeExp ~ year, data = .x)),
tidy = map(fit, broom::tidy)
) %>%
select(tidy) %>%
unnest(tidy)
#> Adding missing grouping variables: `country`
#> # A tibble: 284 x 6
#> # Groups: country [142]
#> country term estimate std.error statistic p.value
#> <fct> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Afghanistan (Intercept) -508. 40.5 -12.5 1.93e- 7
#> 2 Afghanistan year 0.275 0.0205 13.5 9.84e- 8
#> 3 Albania (Intercept) -594. 65.7 -9.05 3.94e- 6
#> 4 Albania year 0.335 0.0332 10.1 1.46e- 6
#> 5 Algeria (Intercept) -1068. 43.8 -24.4 3.07e-10
#> 6 Algeria year 0.569 0.0221 25.7 1.81e-10
#> 7 Angola (Intercept) -377. 46.6 -8.08 1.08e- 5
#> 8 Angola year 0.209 0.0235 8.90 4.59e- 6
#> 9 Argentina (Intercept) -390. 9.68 -40.3 2.14e-12
#> 10 Argentina year 0.232 0.00489 47.4 4.22e-13
#> # … with 274 more rows
Or
gapminder %>%
group_by(country) %>%
do(fit = lm(lifeExp ~ year, data = .)) %>%
do(data.frame(country = .$country, broom::tidy(.$fit)))
This requires:
mutate.rowwise()
needs to automatically wrap in outputs in list where needed.rowwise()
needs to be able to capture grouping variables; orgrouped_df
needs some way to activate row-wise magic?summarise.rowwise()
would returngrouped_df
since might no longer have 1 row per group?- New
nest_by()
that works likegroup_nest()
+rowwise()
. Needs a lot of thinking about name.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
romainfrancois commentedon Jan 9, 2020
if we don't need
fit
to stick around, then we have :Created on 2020-01-09 by the reprex package (v0.3.0.9000)
romainfrancois commentedon Jan 14, 2020
If we were to :
summarise()
ability to return size > 1Then we can have this:
Perhaps with a warning here because we magic enlist a data frame (so a vector) of size > 1.
And we could make it explicit with
results = list(broom::tidy(fit))
instead, but we would not have to make it explicit forfit =
because it does not return a vector, nor we would need to add the[[
as long as we are in thesummarise()
call, as we know we've just made it.This loses some functionality that
summarise()
was supposed to gain, i.e. size > 1 results, but that can be the job ofdo()
which would allow it, and potentially not do group peeling.mutate(.by_row =)
,reframe(.by_row =)
, and possiblyfilter(.by_row =)
#6660