[R] Scanning data files line-by-line
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Apr 30 15:31:08 CEST 2003
On Wed, 30 Apr 2003, R A F wrote:
> 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?
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.
> (b) When readLines is used, a string is returned.
Not quite: a character vector 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?
You could use scan on the line: it works on textConnections.
> Thanks very much again!
>
> >From: Prof Brian Ripley <ripley at stats.ox.ac.uk>
> >To: R A F <raf1729 at hotmail.com>
> >CC: R-help at stat.math.ethz.ch
> >Subject: Re: [R] Scanning data files line-by-line
> >Date: Wed, 30 Apr 2003 14:13:26 +0100 (BST)
> >
> >It's open() you need, as in
> >
> >con <- file("file")
> >open(con)
> >for(i in 1:10) print(readLines(con, n=1))
> >close(con)
> >
> >In C you would need to (f)open a file to read it line-by-line, just as
> >here.
> >
> >The first two lines can be collapsed to
> >
> >con <- file("file", "r")
>
>
> _________________________________________________________________
> http://join.msn.com/?page=features/junkmail
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list