file import
On Jan 18, 2008 6:14 PM, ShyhWeir Tzang <swtzang at gmail.com> wrote:
Dear all:
My data file to be imported has a trailing comment at the end line. However,
when I use read.table to read the file, the error message shows "...line 6
did not have three elements....". How can I import the data file without
having to delete the comment line each time?
header1 header2 header3
100 200 300
400 500 600
700 800 900
<blank>
comments
Thanks very much for any possible help.
Read it once with count.fields and then a second
time with read.table setting nrows. The following
assumes that there is always a blank line following
the data. You may need to generalize this if that's
not always so. (If you can control the imput format
then an even easier thing to do is to ensure that
the comments are prefaced by the comment.char,
an argument to read.table)
Lines <- " header1 header2 header3
100 200 300
400 500 600
700 800 900
comments
"
k <- count.fields(textConnection(Lines), blank.lines.skip = FALSE)
read.table(textConnection(Lines), header = TRUE, nrows = which(k==0)[1]-2)