By Andrie de Vries, Joris Meys

Part of R For Dummies Cheat Sheet

R has many functions that allow you to import data from other applications. The following table lists some of the useful text import functions, what they do, and examples of how to use them.

Function What It Does Example
read.table() Reads any tabular data where the columns are separated (for
example by commas or tabs). You can specify the separator (for
example, commas or tabs), as well as other arguments to precisely
describe your data.
read.table(file=”myfile”, sep=”t”,
header=TRUE)
read.csv() A simplified version of read.table() with all
the arguments preset to read CSV files, like Microsoft Excel
spreadsheets.
read.csv(file=”myfile”)
read.csv2() A version of read.csv() configured
for data with a comma as the decimal point and a semicolon as the
field separator.
read.csv2(file=”myfile”,
header=TRUE)
read.delim() Useful for reading delimited files, with tabs as the default
separator.
read.delim(file=”myfile”,
header=TRUE)
scan() Allows you finer control over the read process when your data
isn’t tabular.
scan(“myfile”, skip = 1,
nmax=100)
readLines() Reads text from a text file one line at a time. readLines(“myfile”)
read.fwf Read a file with dates in fixed-width format. In other words,
each column in the data has a fixed number of characters.
read.fwf(“myfile”,
widths=c(1,2,3)

In addition to these options to read text data, the package foreign allows you to read data from other popular statistical formats, such as SPSS. To use these functions, you first have to load the built-in foreign package, with the following command:

> library("foreign")

The following table lists the functions to import data from SPSS, Stata, and SAS.

Function What It Does Example
read.spss Reads SPSS data file read.spss(“myfile”)
read.dta Reads Stata binary file read.dta(“myfile”)
read.xport Reads SAS export file read.export(“myfile”)