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

Using the by argument for grouping

Finally we get to the last third of the square bracket syntax structure, by. This controls which variable (or variables) to group on. Learning to use this argument will allow us to answer some more interesting questions about my listening habits.

Since each record in our data set is one track listened to, if we group by the artist, and get a count of the number of records in each group, we should get a count of songs listened to by artist. Can you guess how we might do this using data.table?

::author pauses and looks at the reader painfully long::

That’s right! We use the .N special variable while grouping on artist. Let’s do this and chain a call to order the N variable in descending order using order(-N) ...

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