Skip to content

Scanning data files line-by-line

4 messages · R A F, Brian Ripley, Spencer Graves +1 more

#
Thanks very much.  I guess the answer leads to more questions:

(a) What if I don't know the number of lines?  So I would like to use
    a while loop until readLines hits an EOF character.  Would that
    be possible?

(b) When readLines is used, a string is returned.  I'd like to split
    the string into fields, and Andy Liaw suggested strsplit, but the
    number of spaces between fields is variable.  So for example, one
    line could be 1 space 2 space space 3 and the next line could be
    4 space space 5 space 6, so I could not do a strsplit using " ".

    Really what I know is the variable type of each field -- for
    example, each line is double, string, then double, etc.  How
    would one use this information to split the string given by
    readLines?

Thanks very much again!
#
On Wed, 30 Apr 2003, R A F wrote:

            
Yes. After you reach the end of the file you will get character(0) since

Value:

     A character vector of length the number of lines read.

and zero lines would have been read.
Not quite: a character vector is returned.
You could use scan on the line: it works on textConnections.

            
        

  
    
#
With a "connection" instead of a "file", there is no counterpart to 
"count.fields" to summarize what's available?

Thanks,
Spencer Graves
Prof Brian Ripley wrote:
#
On Wed, 30 Apr 2003 07:28:03 -0700, Spencer Graves
<spencer.graves at pdf.com> wrote:

            
The help for count.fields says a connection can be used...

Duncan Murdoch