使用dplyr的do()与summary() [英] using dplyr's do() with summary()

查看:111
本文介绍了使用dplyr的do()与summary()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 dplyr 的拆分应用组合策略来应用 summary()命令



取一个简单的数据框:

  df<  -  data.frame(class = c('A','A','B','B'),
value = c(100,120,800,880))

理想情况下,我们会这样做:

  df%>%
group_by(class)%>%
do(summary(。$ value))
pre>

不幸的是,这不行。有任何想法吗?

解决方案

问题是 dplyr do()只适用于 data.frame 的输入。



扫帚包 tidy()函数可用于将 summary()的输出转换为 data.frame

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

这样:

 来源:本地数据框[2 x 7] 
组: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


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:

df <- data.frame(class = c('A', 'A', 'B', 'B'),
                 value = c(100, 120, 800, 880))

Ideally we would do something like this:

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

Unfortunately this does not work. Any ideas?

解决方案

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

这篇关于使用dplyr的do()与summary()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆