O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Grouping in dplyr

If you recall, the way we performed grouping in data.table was by using the by argument and performing our aggregation in the j section. In dplyr, we perform grouping and aggregation using the group_by and summarise functions, respectively.

As our first example, let’s get sorted count of tracks I’ve listened to by artist. We’ll use the slice function to only return the top 10, as shown in the following code:

> tracks %>%+   group_by(artist) %>%+   summarise(N=n()) %>%+   arrange(desc(N)) %>%+   slice(1:10)# A tibble: 10 x 2   artist                  N   <chr>               <int> 1 Belle and Sebastian  5557 2 The Smiths           4881 3 The Magnetic Fields  4622 4 P:ano                3888 5 The Cure             3792 6 Stars                3554 7 Bad Religion         2977 8 Elvis Costello       2908 9 The Beatles          246110 Radiohead ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required