Home

Importing Data into R

|
|  Updated:  
2016-03-26 07:41:08
|   From The Book:  
Statistical Analysis with R Essentials For Dummies
Explore Book
Buy On Amazon

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")

About This Article

This article is from the book: 

About the book author:

Andrie de Vries is a leading R expert and Business Services Director for Revolution Analytics. With over 20 years of experience, he provides consulting and training services in the use of R.

Joris Meys is a statistician, R programmer and R lecturer with the faculty of Bio-Engineering at the University of Ghent.