[R] skipping specific rows in read.table

David Kane <David Kane a296180 at mica.fmr.com
Mon Apr 22 15:06:40 CEST 2002


We are considering organizing some of our ascii files with multiple "column
names" like so:

a.long.but.complete.name	a.different.complex.name
short.name.1	short.name.2
1	7
2	8
3	9
[more data]

The basic idea is that we want to keep, in one location, both a long descriptive
name of each variable (in row 1) and a short convenient name (in row 2). I
could imagine keeping other information about the data in here as well.

[Comment: Of course, my CS buddies would say that the "grown up" way to handle
this sort of thing is XML, but I haven't seen much discussion of people reading
XML files directly into R -- although Duncan Temple Lang's R/SPlus XML Parsing
Package looks quite interesting. In any event, since I don't have complete
control over the format of these files, my options may be limitted. I would
certainly love to hear about any XML/R success stories.]

In any event, I can easily use the "skip" argument to read.table to ignore the
first row.

> read.delim("/home/a296180/tmp/junk.txt", skip = 1)
  short.name.1 short.name.2
1            1            7
2            2            8
3            3            9

Is there a similar trick for ignoring the second row? That is, I want to
produce a dataframe that looks like:

  a.long.but.complete.name a.different.complex.name
1                        1                        7
2                        2                        8
3                        3                        9

I can think of various hacks, including 1) adding a comment character to the
second row prior to reading it in and 2) reading everything in and then
deleting the second row and, perhaps, coercing the variable to be the
appropriate type and 3) changing the code to read.table itself (which we did
before to handle some issues made moot by Professor Ripley's marvelous
`colClasses' addition to read.table), but I am hoping that there is a better
way. To the extent that it matters, I am using R 1.4.0 patched on Solaris.


Dave Kane
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list