4

I have the following, but want to add the group_by() key Species to the resulting tibble:

MWE

iris %>% 
  group_by(Species) %>% 
  group_map(~ broom::tidy(lm(Sepal.Length ~ Sepal.Width, data = .x))) %>% 
  bind_rows()

Output

# How do I add the grouping key `Species` to this?

  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)    2.64     0.310       8.51 3.74e-11
2 Sepal.Width    0.690    0.0899      7.68 6.71e-10
3 (Intercept)    3.54     0.563       6.29 9.07e- 8
4 Sepal.Width    0.865    0.202       4.28 8.77e- 5
5 (Intercept)    3.91     0.757       5.16 4.66e- 6
6 Sepal.Width    0.902    0.253       3.56 8.43e- 4
mkk
  • 879
  • 6
  • 19

3 Answers3

2

You can use group_modify() instead of group_map().

library(purrr)
library(dplyr)

iris %>% 
   group_by(Species) %>% 
   group_modify(~ broom::tidy(lm(Sepal.Length ~ Sepal.Width, data = .x)))

# A tibble: 6 x 6
# Groups:   Species [3]
  Species    term        estimate std.error statistic  p.value
  <fct>      <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 setosa     (Intercept)    2.64     0.310       8.51 3.74e-11
2 setosa     Sepal.Width    0.690    0.0899      7.68 6.71e-10
3 versicolor (Intercept)    3.54     0.563       6.29 9.07e- 8
4 versicolor Sepal.Width    0.865    0.202       4.28 8.77e- 5
5 virginica  (Intercept)    3.91     0.757       5.16 4.66e- 6
6 virginica  Sepal.Width    0.902    0.253       3.56 8.43e- 4
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
1

We may do this in summarise, return a list column and unnest the output

library(dplyr)
library(tidyr)
iris %>%
    group_by(Species) %>%
    summarise(out = list(broom::tidy(lm(Sepal.Length ~ Sepal.Width, 
           data = cur_data())))) %>% 
    unnest(out)

-output

# A tibble: 6 x 6
  Species    term        estimate std.error statistic  p.value
  <fct>      <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 setosa     (Intercept)    2.64     0.310       8.51 3.74e-11
2 setosa     Sepal.Width    0.690    0.0899      7.68 6.71e-10
3 versicolor (Intercept)    3.54     0.563       6.29 9.07e- 8
4 versicolor Sepal.Width    0.865    0.202       4.28 8.77e- 5
5 virginica  (Intercept)    3.91     0.757       5.16 4.66e- 6
6 virginica  Sepal.Width    0.902    0.253       3.56 8.43e- 4

In group_map, according to documentation, the .y is the key, which we can add as a column

iris %>% 
  group_by(Species) %>% 
  group_map(~ broom::tidy(lm(Sepal.Length ~ Sepal.Width, data = .x)) %>% 
           mutate(Species = .y$Species, .before = 1)) %>% 
  bind_rows()

-output

# A tibble: 6 x 6
  Species    term        estimate std.error statistic  p.value
  <fct>      <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 setosa     (Intercept)    2.64     0.310       8.51 3.74e-11
2 setosa     Sepal.Width    0.690    0.0899      7.68 6.71e-10
3 versicolor (Intercept)    3.54     0.563       6.29 9.07e- 8
4 versicolor Sepal.Width    0.865    0.202       4.28 8.77e- 5
5 virginica  (Intercept)    3.91     0.757       5.16 4.66e- 6
6 virginica  Sepal.Width    0.902    0.253       3.56 8.43e- 4
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Using split + map_df -

library(dplyr)
library(purrr)

iris %>% 
  split(.$Species) %>% 
  map_df(~ broom::tidy(lm(Sepal.Length ~ Sepal.Width, data=.x)),.id = 'Species')

#  Species    term        estimate std.error statistic  p.value
#  <chr>      <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#1 setosa     (Intercept)    2.64     0.310       8.51 3.74e-11
#2 setosa     Sepal.Width    0.690    0.0899      7.68 6.71e-10
#3 versicolor (Intercept)    3.54     0.563       6.29 9.07e- 8
#4 versicolor Sepal.Width    0.865    0.202       4.28 8.77e- 5
#5 virginica  (Intercept)    3.91     0.757       5.16 4.66e- 6
#6 virginica  Sepal.Width    0.902    0.253       3.56 8.43e- 4
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213