Dplyr equivalent to ddply in plyr example

ok, I'm trying to wrap my head around dplyr using it instead of plyr. In my short time with R, I got used to ddply. I am using a "simple" example using dplyr, not ddply in plyr. Here: in the following:

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

      

I get a pivot table of diamond clarity and cut counts. In dplyr, the simplest example I can think of is:

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

      

which seems a little more attractive. Is there a better way to simplify this? ~ thanks

+3


source to share


2 answers


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



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

      

+3


source


In the latest version of dplyr, you can simplify this:

diamonds %>% count(clarity, cut)

      

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



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

      

If you downloaded plyr or rename in your environment, you may need a rename prefix:

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

      

+2


source







All Articles