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 ...