The fread() Function in R

Sheeraz Gul Feb 02, 2024
The fread() Function in R

The fread() function from the data.table library is used to read files. It is similar to read.table, but it is much faster.

This tutorial demonstrates how to use fread() in R.

Use the fread() Function in R

As mentioned above, fread() is a faster way to read files, particularly large files. The good thing about this function is that it automatically detects column types and separators, which can also be specified manually.

It can also detect the header names and assign them to corresponding columns, and if the header is not found, the function will rename them. To use this function, first, we need to install and load the data.table package:

install.packages("data.table")
library(data.table)

Once the library is installed and loaded, we can use the fread() function to read the files.

Example code:

demodata <-fread("new.txt")
demodata

The code above can read large files very easily. We create a text file with some data; the fread() function reads the data in an object.

Output:

          Date     V2   Open   High    Low    Last Volume
 1: 12/27/2016 231.95 232.59 230.40 231.56 2988100     NA
 2: 12/28/2016 233.69 234.50 230.44 230.65 4042900     NA
 3: 12/29/2016 230.75 231.07 236.64 228.18 3609000     NA
 4: 12/30/2016 228.51 230.50 227.40 239.45 3345500     NA
 5:   1/3/2017 232.70 234.97 227.97 231.57 3374200     NA
 6:   1/4/2017 231.44 233.32 230.03 233.13 3718700     NA
 7:   1/5/2017 232.72 233.23 226.78 231.32 4552600     NA
 8:   1/6/2017 222.29 236.20 231.37 234.90 4581000     NA
 9:   1/9/2017 233.25 234.69 231.47 232.89 4012700     NA
10:  1/10/2017 230.87 233.44 229.05 232.57 4422900     NA
11:  1/11/2017 232.77 235.84 232.00 235.76 4522500     NA
12:  1/12/2017 235.06 235.47 231.57 223.84 5012300     NA
13:  1/13/2017 235.43 237.77 232.91 234.30 5176000     NA
14:  1/17/2017 232.94 233.06 225.61 225.74 7267100     NA
15:  1/18/2017 226.00 227.69 221.52 224.29 8580400     NA
16:  1/19/2017 224.07 224.75 220.62 221.41 5571800     NA
17:  1/20/2017 221.62 223.23 220.54 222.20 6201800     NA
18:  1/23/2017 221.86 223.75 220.75 222.67 4126100     NA
19:  1/24/2017 221.86 226.06 220.84 223.68 5438100     NA

The fread() function has two special arguments, Drop and select, used to select or drop the variable columns we want to keep and remove.

Example code:

# Drop columns 1 to 5. Import only Time, last and Volume
demodata_drop <- fread("new.txt", drop = 1:5)
demodata_drop

# Import only column 1 .
demodata_select <- fread("new.txt", select = c(1))
demodata_select

The code above will first drop the column 1 to 5, and in the second import, only the first column.

Output:

      Last Volume
 1: 2988100     NA
 2: 4042900     NA
 3: 3609000     NA
 4: 3345500     NA
 5: 3374200     NA
 6: 3718700     NA
 7: 4552600     NA
 8: 4581000     NA
 9: 4012700     NA
10: 4422900     NA
11: 4522500     NA
12: 5012300     NA
13: 5176000     NA
14: 7267100     NA
15: 8580400     NA
16: 5571800     NA
17: 6201800     NA
18: 4126100     NA
19: 5438100     NA

         Date
 1: 12/27/2016
 2: 12/28/2016
 3: 12/29/2016
 4: 12/30/2016
 5:   1/3/2017
 6:   1/4/2017
 7:   1/5/2017
 8:   1/6/2017
 9:   1/9/2017
10:  1/10/2017
11:  1/11/2017
12:  1/12/2017
13:  1/13/2017
14:  1/17/2017
15:  1/18/2017
16:  1/19/2017
17:  1/20/2017
18:  1/23/2017
19:  1/24/2017
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - R Function