Skip to content

Broadly consider do() and rowwise() #4723

@hadley

Description

@hadley

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; or grouped_df needs some way to activate row-wise magic?
  • summarise.rowwise() would return grouped_df since might no longer have 1 row per group?
  • New nest_by() that works like group_nest() + rowwise(). Needs a lot of thinking about name.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions