How to Use read.table() to Import Tabular Data in R

By Andrie de Vries, Joris Meys

The functions read.csv(), read.csv2(), and read.delim() are special cases of the multipurpose read.table() function in R that can deal with a wide variety of data file formats. The read.table() function has a number of arguments that give you fine control over the specification of the text file you want to import. Here are some of these arguments:

  • header: If the file contains column names in the first row, specify TRUE.

  • sep: The data separator (for example, sep=”,” for CSV files or sep=”t” for tab-separated files).

  • quote: By default, strings are enclosed in double quotation marks (). If the text file uses single quotation marks, you can specify this as the argument to quote (for example, quote=”‘”, a single quote embedded between double quotes).

  • nrows: If you want to read only a certain number of rows of a file, you can specify this by providing an integer number.

  • skip: Allows you to ignore a certain number of lines before starting to read the rest of the file.

  • stringsAsFactors: If TRUE, it converts strings to factors. It’s FALSE by default.

You can access the built-in help by typing ?read.table into your console.