O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

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

How it works...

Data tables in the data.table package outperform the *apply family of functions and the **ply functions. The simple data.table syntax is DT[i,j,by], where the data table DT is the subset using rows in i to calculate j grouped by by.

In step 1, mean(mpg) is calculated, grouped by cylinders for all rows of the data table; omitting i causes all rows of the data table to be included.

To create a new column for the calculated j, just add :=, as in step 2. Here, we added a new column, meanmpg, to the data table to store mean(mpg) for each cylinder type.

By default, with is set to TRUE and j is evaluated for subsets of the data frame. However, if we do not need any computation and just want to retrieve data, then we can specify ...

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