10.2.2 Reading Text Files..............................................
In computer literature, there is often a distinction made betweentext files
andbinary files. That distinction is somewhat misleading—every file is binary
in the sense that it consists of 0s and 1s. Let’s take the termtext fileto mean a
file that consists mainly of ASCII characters or coding for some other human
language (such as GB for Chinese) and that uses newline characters to give
humans the perception of lines. The latter aspect will turn out to be central
here. Nontext files, such as JPEG images or executable program files, are
generally calledbinary files.
You can usereadLines()to read in a text file, either one line at a time or
in a single operation. For example, suppose we have a filez1with the follow-
ing contents:
John 25
Mary 28
Jim 19
We can read the file all at once, like this:
z1 <- readLines("z1")
z1
[1] "John 25" "Mary 28" "Jim 19"
Since each line is treated as a string, the return value here is a vector of
strings—that is, a vector of character mode. There is one vector element for
each line read, thus three elements here.
Alternatively, we can read it in one line at a time. For this, we first need
to create a connection, as described next.
10.2.3 Introduction to Connections......................................
Connectionis R’s term for a fundamental mechanism used in various kinds of
I/O operations. Here, it will be used for file access.
The connection is created by callingfile(),url(), or one of several other
R functions. To see a list of those functions, type this:
?connection
So, we can now read in thez1file (introduced in the previous section)
line by line, as follows:
c <- file("z1","r")
readLines(c,n=1)
[1] "John 25"
readLines(c,n=1)
[1] "Mary 28"
readLines(c,n=1)
[1] "Jim 19"
Input/Output 237