The scan() function

 Suppose the data vectors are of equal length and are to be read in in parallel. Further suppose that there are three vectors, the first of mode character and the remaining two of mode numeric, and the file is input.dat. The first step is to use scan() to read in the three vectors as a list, as follows

in <- scan("input.dat", list("",0,0))

The second argument is a dummy list structure that establishes the mode of the three vectors to be read. The result, held in in, is a list whose components are the three vectors read in. To separate the data items into three separate vectors, use assignments like

label <- in[[1]]; x <- in[[2]]; y <- in[[3]]

More conveniently, the dummy list can have named components, in which case the names can be used to access the vectors read in. For example

in <- scan("input.dat", list(id="", x=0, y=0))

If you wish to access the variables separately they may either be re-assigned to variables in the working frame:

label <- in$id; x <- in$x; y <- in$y

or the list may be attached at position 2 of the search list, (see §[*]).

If the second argument is a single value and not a list, a single vector is read in, all components of which must be of the same mode as the dummy value.

X <- matrix(scan("light.dat", 0), ncol=5, byrow=T)

There are more elaborate input facilities available and these are detailed in the manual.



Jeff Banfield
2/13/1998