[R] Scanning data files line-by-line

R A F raf1729 at hotmail.com
Wed Apr 30 20:06:56 CEST 2003


I'm sorry to have to ask another question related to this.  What if
the lines have variable number of fields?

For example, all I know is that each line starts with double, string,
double, say, but some lines may have some more fields afterwards.  So
the data file may look like

1 A 2
3 B 4 5
6   DD 7
etc.

If I use scan with list( 0, "", 0 ), each line is treated as if it
has a multiple of 3 elements, but really, what I want is to discard
all fields after the third.

I tried the nmax = 3 option but that did not seem to work.  Maybe I'm
doing this wrong.

Thanks again!

>From: "R A F" <raf1729 at hotmail.com>
>To: spencer.graves at pdf.com, ripley at stats.ox.ac.uk
>CC: R-help at stat.math.ethz.ch
>Subject: Re: [R] Scanning data files line-by-line
>Date: Wed, 30 Apr 2003 15:21:23 +0000
>
>Hi all, thanks to everyone again for helping out.  I don't want to
>generate too many messages, but this problem seems common enough that
>maybe it's worth a summary.
>
>What I can do is this.  Let's say "file" has lines of double, string,
>double with variable number of spaces between fields followed by EOF.
>
>aaa <- file( "file", "r" )
>
>while( length( ( x <- scan( aaa, nlines = 1, list( 0, "", 0 ) ) )[[1]] )
> > 0 )
>{
>   check to see if x is empty again (by length( x[[1]] ) > 0 ) since
>   we would read in the EOF character into x still
>
>   if not empty
>      start processing
>}
>
>close( aaa )
>
>Here x is a list and x[[1]] is the first field, etc.
>
>Professor Ripley also suggested textConnections, but I didn't
>experiment -- I'm usually happy to find something that works.  :-)
>
>Thanks again.



More information about the R-help mailing list