Code Studio: Group By with data.table
Do in data.table what The data.table Format teaches: DT[i, j, by].
The program reads the name of a column to group by: gear, cyl or am.
- Convert
mtcarsto a data.table withas.data.table(). - For each group, compute the number of cars (
.N) and the meanmpgrounded to 1 decimal place. Call the columnscarsandmpg. - Sort by the grouping column and print the result.
Hint: by = group_col works when group_col holds a column name as text, and setorderv(result, group_col) sorts by it.
Do in data.table what The data.table Format teaches: DT[i, j, by].
The program reads the name of a column to group by: gear, cyl or am.
- Convert
mtcarsto a data.table withas.data.table(). - For each group, compute the number of cars (
.N) and the meanmpgrounded to 1 decimal place. Call the columnscarsandmpg. - Sort by the grouping column and print the result.
Hint: by = group_col works when group_col holds a column name as text, and setorderv(result, group_col) sorts by it.