[R] How to read malformed csv files with read.table?

Martin Ballaschk tmp082008 at ballaschk.com
Fri Aug 22 19:59:28 CEST 2008


Hi Brian,


Am 22.08.2008 um 19:22 schrieb Prof Brian Ripley:
> On Fri, 22 Aug 2008, Martin Ballaschk wrote:
>> My solution is reading the files without the headers (skip = 1) and  
>> seperately reading the headers with scan (scan("myfile.CSV", what =  
>> "character", sep = "\t", nlines = 1). After throwing out the first  
>> two columns it should be possible to assign the scanned colnames to  
>> the data.frame colnames.
>
> Yes, but if you read the header first you can set the col.names via  
> the arg to read.table().

Thanks! I plan to do it like that (actually it will be stuffed into a  
loop to read a bunch of files), seems to work:

 > headernames <- scan("test.CSV", what = "character", sep = "\t",  
nlines = 1, skip = 4)
 > my.table <- read.table("test.CSV", header=F, skip = 5, col.names =  
c("crap.1", "crap.2", headernames))

 > head(my.table)
   crap.1 crap.2 time.ms C550.KMS Cyt_b559.KMS [...] etc.
1      0 Point1  -599.5    0.000        0.000
2      0 Point2  -598.0    0.019       -0.014
3      0 Point3  -596.5    0.025       -0.023
4      0 Point4  -595.0    0.034       -0.029
5      0 Point5  -593.5    0.049       -0.033
6      0 Point6  -592.0    0.068       -0.033

Cheers
Martin



More information about the R-help mailing list