[R] Omitting blank lines with read.table

Patrick Connolly p.connolly at hortresearch.co.nz
Wed Sep 24 01:32:26 CEST 2003


Say we have a tab delimited file called bug.txt

Part	Rep	Cage	Hb pupae
1	1	S	32
	1	M	34
		L	42
			  
			  
	2	S	36
		M	28
		L	36

read.delim("bug.txt")

  Part Rep Cage Hb.pupae
1    1   1    S       32
2   NA   1    M       34
3   NA  NA    L       42
4   NA  NA            NA
5   NA  NA            NA
6   NA   2    S       36
7   NA  NA    M       28
8   NA  NA    L       36
>

Variations on read.table give the same result.

When I first used read.table in Splus, I liked the way it ignored rows
that were empty (at least when using sep = "\t").  A line was
considerend empty if it contained only tab characters, so the rows of
NAs or ""s are omitted, so that rows 4 and 5 above would be deleted.

R's read.table differs in this respect (and a number of really neat
ones).  I probably know enough Perl to be able to write a short script
that could delete such lines, and it's not difficult to remove the
rows from the dataframe afterwards; but maybe there's something simple
I've misunderstood in the use of R's read.table.

I can't use na.omit since the other NAs in the data can be dealt with
so I don't want them removed.  Other suggestions welcome.

Thanks

-- 
Patrick Connolly
HortResearch
Mt Albert
Auckland
New Zealand 
Ph: +64-9 815 4200 x 7188
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it.  ---Steven Wright 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~




More information about the R-help mailing list