[R] Partially reading a file (particularly)

Gabor Grothendieck ggrothendieck at gmail.com
Wed May 30 03:21:25 CEST 2007


On 5/29/07, Charles C. Berry <cberry at tajo.ucsd.edu> wrote:
> On Tue, 29 May 2007, Tobin, Jared wrote:
>
> > Hello,
> >
> > I am trying to figure out if there exists some R command that allows one
> > to be
> > particularly selective when reading a file.  I'm dealing with large
> > fixed-width data
> > sets that look like
> >
> > 539001..
> > 639001..
> > 639001..
> > ...
> > 539002..
> > 639002..
> > ...
> >
> > Presently, I am using read.fwf to read an entire file, but I am
> > interested only in
> > reading those records beginning with 5.  I have been unable to find help
> > in any of
> > the suggested resources.
>
> Assuming you have 'grep' in your path,
>
>        res <- read.fwf( pipe( "grep '^5' my.file" ) , <other args> )
>
> will do it.
>
> grep will usually be found on linux/unix systems and Mac OS X. The
> 'Rtools' toolkit for windows has grep, I believe.

On windows XP we can also use findstr which comes with Windows:

         res <- read.fwf( pipe( "findstr /b 5 my.file" ) , <other args> )



More information about the R-help mailing list