The Art of R Programming

(WallPaper) #1

Theintextract()function itself is a straightforward conversion of charac-
ters to numbers, such as converting the string"12"to the number 12.
Note that, if not for the presence of Household records, we could do all
of this much more easily with a handy built-in R function:read.fwf(). The
name of this function is an abbreviation for “read fixed-width formatted,”
alluding to the fact that each variable is stored in given character positions
of a record. In essence, this function alleviates the need to write a function
likeintextract().


10.2.5 Accessing Files on Remote Machines via URLs.....................


Certain I/O functions, such asread.table()andscan(), accept web URLs as
arguments. (Check R’s online help facility to see if your favorite function
allows this.)
As an example, we’ll read some data from the University of Califor-
nia, Irvine archive athttp://archive.ics.uci.edu/ml/datasets.html, using the
Echocardiogramdata set. After navigating the links, we find the location of
that file and then read it from R, as follows:



uci <- "http://archive.ics.uci.edu/ml/machine-learning-databases/"
uci <- paste(uci,"echocardiogram/echocardiogram.data",sep="")
ecc <- read.csv(uci)



(We’ve built up the URL in stages here to fit the page.)
Let’s take a look at what we downloaded:



head(ecc)
X11 X0 X71 X0.1 X0.260 X9 X4.600 X14 X1 X1.1 name X1.2 X0.2
1 19 0 72 0 0.380 6 4.100 14 1.700 0.588 name 1 0
2 16 0 55 0 0.260 4 3.420 14 1 1 name 1 0
3 57 0 60 0 0.253 12.062 4.603 16 1.450 0.788 name 1 0
4 19 1 57 0 0.160 22 5.750 18 2.250 0.571 name 1 0
5 26 0 68 0 0.260 5 4.310 12 1 0.857 name 1 0
6 13 0 62 0 0.230 31 5.430 22.5 1.875 0.857 name 1 0



We could then do our analyses. For example, the third column is age, so
we could find its mean or perform other calculations on that data. See the
echocardiogram.names page athttp://archive.ics.uci.edu/ml/machine-learning-
databases/echocardiogram/echocardiogram.namesfor descriptions of all of the
variables.


10.2.6 Writing to a File................................................


Given the statistical basis of R, file reads are probably much more common
than writes. But writes are sometimes necessary, and this section will present
methods for writing to files.


Input/Output 243
Free download pdf