使用dplyr的do()与summary() [英] using dplyr's do() with summary()
本文介绍了使用dplyr的do()与summary()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够使用 dplyr
的拆分应用组合策略来应用 summary()
命令
取一个简单的数据框:
df< - data.frame(class = c('A','A','B','B'),
value = c(100,120,800,880))
理想情况下,我们会这样做:
df%>%
pre>
group_by(class)%>%
do(summary(。$ value))
不幸的是,这不行。有任何想法吗?
解决方案问题是
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 thesummary()
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
'sdo()
only works with with input of the formdata.frame
.The broom package's
tidy()
function can be used to convert outputs ofsummary()
todata.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屋!
查看全文