using dplyr's do() with summary()

后端 未结 3 1835
清酒与你
清酒与你 2021-01-05 20:28

I would like to be able to use dplyr\'s split-apply-combine strategy to the apply the summary() command.

Take a simple data frame:

3条回答
  •  灰色年华
    2021-01-05 20:54

    The problem is that dplyr's do() only works with with input of the form data.frame.

    The broom package's tidy() function can be used to convert outputs of summary() to data.frame.

    df %>%
      group_by(class) %>%
      do( tidy(summary(.$value)) )
    

    This gives:

    Source: local data frame [2 x 7]
    Groups: class [2]
    
       class minimum    q1 median  mean    q3 maximum
      (fctr)   (dbl) (dbl)  (dbl) (dbl) (dbl)   (dbl)
    1      A     100   105    110   110   115     120
    2      B     800   820    840   840   860     880
    

提交回复
热议问题