【问题标题】:how to add key variables to `dplyr::group_map()`?如何将关键变量添加到`dplyr::group_map()`?
【发布时间】:2021-11-03 03:52:09
【问题描述】:

我有以下内容,但想将 group_by()Species 添加到生成的 tibble:

MWE

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

输出

# 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

【问题讨论】:

    标签: r dplyr split-apply-combine


    【解决方案1】:

    我们可以在summarise 中执行此操作,返回list 列和unnest 输出

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

    -输出

    # 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
    

    group_map 中,根据文档,.y 是键,我们可以将其添加为列

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

    -输出

    # 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
    

    【讨论】:

      【解决方案2】:

      您可以使用group_modify() 代替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
      

      【讨论】:

        【解决方案3】:

        使用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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-14
          • 2020-11-22
          • 2019-09-13
          • 1970-01-01
          • 2010-11-09
          • 1970-01-01
          • 2016-01-30
          • 2011-11-01
          相关资源
          最近更新 更多