[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-sig-finance
Subject:    Re: [R-SIG-Finance] file import
From:       "Gabor Grothendieck" <ggrothendieck () gmail ! com>
Date:       2008-01-19 1:11:38
Message-ID: 971536df0801181711y6c7ec712w7aa6ed919bcba740 () mail ! gmail ! com
[Download RAW message or body]

On Jan 18, 2008 6:14 PM, ShyhWeir Tzang <swtzang@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)

_______________________________________________
R-SIG-Finance@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. 
-- If you want to post, subscribe first.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic