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

Duncan Murdoch murdoch at stats.uwo.ca
Mon Oct 25 19:44:31 CEST 2004


On Mon, 25 Oct 2004 15:56:37 +0200, Henrik Andersson
<h.andersson at nioo.knaw.nl> wrote :

>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
>...
>@@Start Values@@
>	Column1 Column2 Column3 ...
>Row1	1	2	3 ...
>...
>@@End Values@@
>
>More stuff
>...
>@@Start OtherValues@@
>	Column1 Column2 Column3 ...
>Row1	1	2	3 ...
>...
>@@End OtherValues@@
>
>
>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?

I would avoid putting mixing multiple tables in the same file.  I
think you'll run into fewer problems if you put each table into a
separate file, and generate an index file to list all the tables.
Each of the files in your scheme would then become a subdirectory in
my scheme.

If the multiplicity of files is a problem, you could use zip or winzip
to put them all into a zip file; R can extract a file from one of
those using zip.file.extract.

Duncan Murdoch




More information about the R-help mailing list