Importing Data from External Files
One of the nicest things about R is how easy it is to pull in data from other programs. R can import data from text files, other statistics software, and even spreadsheets. You don’t even need a local copy of the file: you can specify a file at a URL, and R will fetch the file for you over the Internet.
Text Files
Most text files containing data are formatted similarly: each line of a text file represents an observation (or record). Each line contains a set of different variables associated with that observation. Sometimes, different variables are separated by a special character called the delimiter. Other times, variables are differentiated by their location on each line.
Delimited files
R includes a family of functions for importing
delimited text files into R, based on the read.table
function:
read.table(file, header, sep = , quote = , dec = , row.names, col.names, as.is = , na.strings , colClasses , nrows =, skip = , check.names = , fill = , strip.white = , blank.lines.skip = , comment.char = , allowEscapes = , flush = , stringsAsFactors = , encoding = )
The read.table
function
reads a text file into R and returns a data.frame
object. Each row in the input
file is interpreted as an observation. Each column in the input
file represents a variable. The read.table
function expects each field
to be separated by a delimiter.
For example, suppose that you had a file called top.5.salaries.csv that contained the following text (and only this text):
name.last,name.first,team ...
Get R in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.