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
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 to share