Reading and Writing Files
Now that we’ve covered the basics of I/O, let’s get to some more practical applications of reading and writing files. The following sections discuss reading data frames or matrices from files, working with text files, accessing files on remote machines, and getting file and directory information.
Reading a Data Frame or Matrix from a File
In Section 5.1.2, we discussed the use of the function
read.table() to read in a data frame. As a quick review, suppose the file z looks like this:
name age John 25 Mary 28 Jim 19
The first line contains an optional header, specifying column names. We could read the file this way:
> z <- read.table("z",header=TRUE) > z name age 1 John 25 2 Mary 28 3 Jim 19
scan() would not work ...