Skip to Content
Data Analysis with R, Second Edition - Second Edition
book

Data Analysis with R, Second Edition - Second Edition

by Tony Fischetti
March 2018
Beginner to intermediate content levelBeginner to intermediate
570 pages
13h 42m
English
Packt Publishing
Content preview from Data Analysis with R, Second Edition - Second Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Exploratory Data Analysis with R

Hands-On Exploratory Data Analysis with R

Radhika Datar, Harish Garg
Bayesian Data Analysis, Third Edition, 3rd Edition

Bayesian Data Analysis, Third Edition, 3rd Edition

Andrew Gelman, John B. Carlin, Hal S. Stern, David B. Dunson, Aki Vehtari, Donald B. Rubin
R: Data Analysis and Visualization

R: Data Analysis and Visualization

Tony Fischetti, Brett Lantz, Jaynal Abedin, Hrishi V. Mittal, Bater Makhabel, Edina Berlinger, Ferenc Illés, Milán Badics, Ádám Banai, Gergely Daróczi, Barbara Dömötör, Gergely Gabler, Dániel Havran, Péter Juhász, István Margitai, Balázs Márkus, Péter Medvegyev, Julia Molnár, Balázs Árpád Szucs, Ágnes Tuza, Tamás Vadász, Kata Váradi, Ágnes Vidovics-Dancs

Publisher Resources

ISBN: 9781788393720Supplemental Content