[R] stacking imported data

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Nov 2 00:35:45 CET 2004

Hi all,
   I have a question that I don't have a good answer for (note the word 
"good"; I have an answer, but I consider it not "good"). Take the 
following data in a single tab-delimited text file:


Labels	Value	SE	2.5%	97.5%
R90	0.231787	1.148044	0.035074	1.531779
R0	0.500861	0.604406	0.185336	1.353552

Labels	Value	SE	2.5%	97.5%
(Intercept)	1.367514	0.036431	1.287975	1.451964

(Note: the <text> tags are not present and are added here only to show 
blank lines.)

I would like to read the data into a single data.frame which looks like

Labels	Value	SE	2.5%	97.5%
A.R90	0.231787	1.148044	0.035074	1.531779
A.R0	0.500861	0.604406	0.185336	1.353552
B.(Intercept)	1.367514	0.036431	1.287975	1.451964

A few rules:

1. the number of rows in "A" and "B" will vary from 1 to ???. Here "A" 
has 1 row (excluding header) and B has 2 rows (excluding header).
2. the number of columns in "A" and "B" will always be the same.
4. the headers for "A" and "B" will always be the same.
3. there is always an empty line at the beginning of the file and in 
between "A" and "B".

My solution involves scan and indexing though it is error prone and not 
flexible if more or less than 5 columns are present in the data. While 
the number of columns is always the same from "A" to "B" it may change 
that "A" and "B" have more or fewer columns.

I hope this makes sense.



More information about the R-help mailing list