dplyr equivalent to ddply in plyr diamonds example

蓝咒 提交于 2020-01-02 04:38:08

问题


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


回答1:


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()) 



回答2:


In the latest version of dplyr you can simplify that down to this:

diamonds %>% count(clarity, cut)

Or if you want to keep the column name 'nrow':

diamonds %>% count(clarity, cut) %>% rename(nrow = n)

If you've got plyr or rename loaded in your environment then you might need to prefix the rename:

diamonds %>% count(clarity, cut) %>% dplyr::rename(nrow = n)


来源:https://stackoverflow.com/questions/25787435/dplyr-equivalent-to-ddply-in-plyr-diamonds-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!