How to Traverse a List or Data Frame with R Apply Functions
How to Store and Calculate Values in R
How to Use Standard Operations in a Matrix in R

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

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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Test Data Proportions with R
How to Structure Your Code in R
How to Use Functions as Arguments in R
How to Substitute Text in R
How to Successfully Follow Naming Conventions in R
Advertisement

Inside Dummies.com