Get startedGet started for free

data.table: fread

1. data.table: fread

The other package I wanted to discuss is data table.

2. data.table

The key performance metric for the authors of data table, Matt Dowle and Arun Srinivasan, is speed. The package is mainly about data manipulation in R, but also features a super powerful function to read your data into R: fread. If you have huge files you have to import and munge in R, you'll want to go with the data table package. Let's start with installing and loading the library. If you want to learn everything about data.table, DataCamp offers course material dedicated to this package. In this course, we'll stick to the fread function. It is pretty similar to the "read-dot-table" function you've seen before but it is more convenient to use. Let me introduce it with an example.

3. fread()

Suppose we have two versions of the states csv file, one with and one without column names in the first row.

4. fread()

Let's call fread on both these csv files, without additional arguments. fread automatically deals with these two different cases. In the first case, the column names are correctly transfered to the data frame, while in the second case, fread made up some column names itself.

5. fread()

Next to automatically handling the names, fread can also infer the column types and the field separators without having to specify these. That's the cool thing about fread: it just works. On top of that, it's ridiculously fast. Of course, the fread function does much more than automatically finding out all column types and conventions in the files you want to read. You can also manually specify the separator, the colClasses, the number of lines to skip and the number of lines to read in manually. Think of fread as an improved version of read-dot-table which is faster, more convenient and adds functionality.

6. Let's practice!

With these pointers on data.table it's time for you to get some hands-on practice yourself. Enjoy!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.