dplyr等同于plyr钻石的例子 [英] dplyr equivalent to ddply in plyr diamonds example

查看:98
本文介绍了dplyr等同于plyr钻石的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我试图把我的头围绕在dplyr上,使用它而不是plyr。在短短的时间里,我已经渐渐变得有些习惯了。我正在使用一个简单的例子来说明如何使用dplyr而不是在plyr中使用ddply。这里:在以下内容中:

  t1.table<  -  ddply(diamonds,c(clarity,cut ),nrow)

我通过清晰度和剪切方式收到钻石计数的汇总表。在dplyr中,我可以想出的最简单的例子是:

  diamonds%>%select(clarity,cut)%> ;%group_by(清晰度,剪切)%>%
总结(count = n()) - > t2.table

这似乎有点参与。有没有更好的方式来简化? 〜谢谢

解决方案

感谢您的帮助。我喜欢这个答案不像原来的ddply命令那么紧凑,但是更容易读取。 (旁注:回答一个问题是一个痛苦,需要工作)

  t3.table<  -  diamonds%>%group_by (清晰度,剪切)%>%summary(nrow = n())


ok, I'm trying to wrap my head around dplyr, using it instead of plyr. In my short time with R I've grown somewhat accustomed to ddply. I'm using a "simple" example for how to use dplyr as opposed to ddply in plyr. Here goes: in the following:

t1.table <- ddply(diamonds, c("clarity", "cut"), "nrow")  

I receive a summary table of counts of diamonds by clarity and cut. In dplyr, the simplest example I can come up with is:

diamonds %>% select(clarity, cut) %>% group_by(clarity, cut) %>%  
    summarise(count=n()) -> t2.table  

which seems a bit more involved. Is there a better way to simplify this? ~ thanks

解决方案

Thanks for the help. I like this answer. Not quite as compact as the original ddply command, but a heck of a lot more readable. (side note: answering a question is a pain, needs work)

    t3.table <- diamonds %>% group_by(clarity, cut) %>% summarise(nrow=n()) 

这篇关于dplyr等同于plyr钻石的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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