[R] csv file with two header rows

William Dunlap wdunlap at tibco.com
Sat Apr 27 16:57:33 CEST 2013


> And I've read them with the 2-calls-to-read.table trick you mentioned
>   > hd <- read.csv(..., nrows=2)
>   > df <- read.csv(..., skip=2, header=FALSE)
>   > names(df) <- ... some operation on hd ...
> (or 3 calls, when I wanted to paste the units onto the variable name.)

Or with read.table(text=readLines("http://...")[-2]):

  > u <- "http://www.ndbc.noaa.gov/data/5day2/SISW1_5day.cwind"
  > d <- read.table(text=readLines(u)[-2], header=TRUE, comment.char="", check.names=FALSE)
  > d[1:4,]
     #YY MM DD hh mm WDIR WSPD GDR  GST GTIME
  1 2013  4 27 14  0  190  4.1 200  5.7  1337
  2 2013  4 27 13 50  198  4.1 999 99.0  9999
  3 2013  4 27 13 40  196  4.1 999 99.0  9999
  4 2013  4 27 13 30  202  4.1 999 99.0  9999
  > with(d, plot(complex(mod=WSPD, arg=(90-WDIR)/180*pi), asp=1))

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: William Dunlap
> Sent: Saturday, April 27, 2013 7:36 AM
> To: 'peter dalgaard'; John Kane
> Cc: r-help at r-project.org; analyst41 at hotmail.com
> Subject: RE: [R] csv file with two header rows
> 
> > But it is not clear to
> > me what is supposed to happen with the the double header, or what the file format in
> > question is exactly.
> 
> I've seen double headers in which the first line gives the names of the variables
> and the second their units.  E.g.,
>   http://www.ndbc.noaa.gov/data/5day2/SISW1_5day.cwind
> contains wind data (from Smith Island, Washington) and it currently starts with
>   #YY  MM DD hh mm WDIR WSPD GDR GST GTIME
>   #yr  mo dy hr mn degT m/s degT m/s hhmm
>   2013 04 27 14 00 190  4.1 200  5.7 1337
>   2013 04 27 13 50 198  4.1 999 99.0 9999
>   2013 04 27 13 40 196  4.1 999 99.0 9999
>   2013 04 27 13 30 202  4.1 999 99.0 9999
>   2013 04 27 13 20 199  3.6 999 99.0 9999
>   2013 04 27 13 10 188  3.1 999 99.0 9999
>   2013 04 27 13 00 182  3.1 210  4.1 1218
>   2013 04 27 12 50 198  3.1 999 99.0 9999
> And I've read them with the 2-calls-to-read.table trick you mentioned
>   > hd <- read.csv(..., nrows=2)
>   > df <- read.csv(..., skip=2, header=FALSE)
>   > names(df) <- ... some operation on hd ...
> (or 3 calls, when I wanted to paste the units onto the variable name.)
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
> 
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> > Of peter dalgaard
> > Sent: Saturday, April 27, 2013 12:48 AM
> > To: John Kane
> > Cc: r-help at r-project.org; analyst41 at hotmail.com
> > Subject: Re: [R] csv file with two header rows
> >
> >
> > On Apr 26, 2013, at 14:48 , John Kane wrote:
> >
> > > I don't think so. read.csv is a striped down version of read.table.  You should be able
> to
> > do this with the skip option there.
> >
> > You can also pass skip= to read.csv, it just passes it on to read.table. But it is not clear
> to
> > me what is supposed to happen with the the double header, or what the file format in
> > question is exactly.
> >
> > It is, e.g., quite possible to do something like
> >
> > hd <- read.csv(..., nrows=2)
> > df <- read.csv(..., skip=2, header=FALSE)
> > names(df) <- ... some operation on hd ...
> >
> > >
> > > John Kane
> > > Kingston ON Canada
> > >
> > >
> > >> -----Original Message-----
> > >> From: analyst41 at hotmail.com
> > >> Sent: Thu, 25 Apr 2013 18:35:42 -0700 (PDT)
> > >> To: r-help at r-project.org
> > >> Subject: [R] csv file with two header rows
> > >>
> > >> Is there a way to use read.csv() on such a file without deleting one
> > >> of the header rows?
> > >>
> > >> Thanks.
> > >>
> > >> ______________________________________________
> > >> R-help at r-project.org mailing list
> > >> https://stat.ethz.ch/mailman/listinfo/r-help
> > >> PLEASE do read the posting guide
> > >> http://www.R-project.org/posting-guide.html
> > >> and provide commented, minimal, self-contained, reproducible code.
> > >
> > > ____________________________________________________________
> > > GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at
> > http://www.inbox.com/smileys
> > > Works with AIM(r), MSN(r) Messenger, Yahoo!(r) Messenger, ICQ(r), Google Talk(tm) and
> > most webmails
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > --
> > Peter Dalgaard, Professor,
> > Center for Statistics, Copenhagen Business School
> > Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> > Phone: (+45)38153501
> > Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list