R Tutorial: Data Manipulation with data.table in R | Intro

Arun Srinivasan introduces data.table, an R package for fast data manipulation. The core syntax uses i for row filtering, j for column selection, and by for grouped operations. data.table outperforms other packages on benchmarks and supports advanced features like rolling joins, reference updates, and fast reshaping.
Chapters
Welcome to the new and renewed introductory course on R's Data Table package. I am Arun Srinivasan, a data scientist in the finance industry. We hope that you are already familiar with data frames at this point. A data table is also a dataframe, but does so much more like data frames. They are columnar data structures and all columns must be of equal length. So why do you need data tables? A data table is a 2D data structure, the two dimensions being rows and columns. However, most data analysis tasks require performing operations by groups. It is quite common to consider grouping as a virtual third dimension. The data table syntax is quite powerful because it provides quick access to these dimensions in the form of placeholders for operations on rows, columns and rows groups. The first argument I allows for filtering of required rows by accepting an expression or simply the required row numbers. If the I argument is empty, then no rows are filtered. The second argument j, operates on columns. In addition to just selecting columns as in a data frame, it also allows for directly computing on the columns as you will see in the next chapters. The last argument by allows you to operate on columns by groups. Data table is also very fast. Many operations are parallelized including filtering, ordering, grouping, file reading, writing, etc. Check out this link for up to date benchmark on data Tables performance against other common packages. Finally, Data table has many additional powerful features including rolling, overlapping and non equi joins. Updating tables by reference, fast reshaping, parallel file reading and writing, primary key based joins, automatic creation of secondary keys for faster filtering and joins, etc. We will not cover joins in this course, but they are covered in great detail in the Joining Data in R with Data Table course. Here on datacamp there are at least three ways in which you can create data tables. In this video we will cover the first two using the data table and as data table functions. We will cover fread in the final chapter of this course you can use the data table function to create a data table from scratch the same way you would use the data frame function. All you need to do is pass vectors of same length to the datatable function. To convert an existing R object to a data table, you can use the as datatable function. As you can see here, we converted the list y to a data table. As mentioned earlier, a data table is also a dataframe and you can confirm that here from the output of class and thus you can use all the functions you would use on a dataframe on a data table. Endrow, ncol and dim for example return the number of rows, columns and dimensions of a data table as it would on a data frame. However, there are a few enhancements. Unlike a dataframe, a data table doesn't automatically convert characters to factors, thus preventing bugs by avoiding unexpected behavior. Also, a data table never sets or uses the row names. Finally, a minor but useful feature is that when you print a data table, a colon is added after the row number to visually separate it from the first column. Now it's your turn to create.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







