Session Ready
Exercise

The by basics

In this section you were introduced to the last of the main parts of the data.table syntax: by. If you supply a j expression and a by list of expressions, the j expression is repeated for each by group. Time to master the by argument with some hands-on examples and exercises.

First, just print iris to the console and observe that all rows are printed and that the column names scroll off the top of your screen. This is because iris is a data.frame. Scroll back up to the top to see the column names.

Instructions
100 XP
  • Convert the iris dataset to a data.table DT. You're now ready to use data.table magic on it!
  • Create a new column containing the mean Sepal.Length for each Species. Do not provide a name for this newly created column.
  • Do exactly the same as in the instruction above, but this time, group by the first letter of the Species name instead. Use substr() for this.