[R] Reading sections of data files based on pattern matching

Gabor Grothendieck ggrothendieck at myway.com
Mon Oct 25 17:06:04 CEST 2004


Henrik Andersson <h.andersson <at> nioo.knaw.nl> writes:

: 
: I am about to write general functions to read the output of simulations 
: models.
: 
: These model generate output files with different sections which I want 
: to analyze plot etc.
: 
: Since this will be used many people at the department I wanted to make 
: sure that will do this in the best way.
: 
: For instance I want to read a snippets of data from a text that look 
: like this.
: -------------------------------
: Lots of stuff
: ...
:  <at>  <at> Start Values <at>  <at> 
: 	Column1 Column2 Column3 ...
: Row1	1	2	3 ...
: ...
:  <at>  <at> End Values <at>  <at> 
: 
: More stuff
: ...
:  <at>  <at> Start OtherValues <at>  <at> 
: 	Column1 Column2 Column3 ...
: Row1	1	2	3 ...
: ...
:  <at>  <at> End OtherValues <at>  <at> 
: 
: 
: I looked in the help files and found grep which operates on character 
: strings, do I have to like this then?
: 
: 1. Read file with readLines("foo.txt")
: 2. grep this object for the start and end of each section ->startline & 
: stopline
: 3. Read the file again with 
: read.table("foo.txt",skip=startline,nrows=stoplin-startline)
: 
: Or is there a more beautiful way?


You could adapt the following to your situation (i.e. multiple sections
rather than just one):

https://www.stat.math.ethz.ch/pipermail/r-help/2003-November/040184.html

Also regarding your example, one potential gotcha to be aware of is
that skip= skips lines but nrow= counts rows of the data frame so they 
are slightly different concepts.




More information about the R-help mailing list