[R] read.fwf and header

Greg Snow Greg.Snow at intermountainmail.org
Wed Nov 1 19:43:36 CET 2006

How about using a connection and reading the header separate from the
data, like this:

tmp1 <- file('c:/temp/tmp.dat')

my.names <- scan(tmp1, nlines=1, what='')

new.data<-read.fwf(file=tmp1, widths=c(3, 4, 10, 3, 2, 2, 2, 2, 11, 19),

names(new.data) <- my.names


Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gregor Gorjanc
Sent: Monday, October 30, 2006 3:33 PM
To: Daniel Nordlund
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] read.fwf and header

Daniel Nordlund wrote:
> Gregor,
> According to the help for read.fwf, sep needs to be set to a value
that occurs only in the header record.  I changed the spaces to commas
in the header record of your example and used the following syntax and
was able to read the file just fine.
> new.data<-read.fwf(file="test.txt", widths=c(3, 4, 10, 3, 2, 2, 2, 2,
11, 19),
>   header=TRUE, sep=',')
> Hope this is helpful,
> Dan

Thanks Dan! But I have to modfy file first. Not that much of work but

Regards, Gregor

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list