[R] Column naming issues using read.table

John Kane jrkrideau at yahoo.ca
Thu Dec 24 16:22:06 CET 2009


I'm not exactly an expert so this is not likely a good way to do it but if the actual variable names are constant across the files why not just read in the data and assign the names later?

see skip in ?read.table.

x  <- read.table("d:/junk1.txt", skip=2)

should read in the data.  


You can clean up the file(s) for length, etc once you have read it/them in.  Using your sample data I drop column one and only keep 2 of the three lines of data.

xx <- xx[1:2, -1]

names(xx)  <-c("A", "B","C","D","E", "F")




--- On Wed, 12/23/09, arthurbeer01 <arthur_beer at hotmail.com> wrote:

> From: arthurbeer01 <arthur_beer at hotmail.com>
> Subject: [R]  Column naming issues using read.table
> To: r-help at r-project.org
> Received: Wednesday, December 23, 2009, 8:31 PM
> 
> Hi, this is my first post so please be gentle.
> I quite new to R and using it for my biology degree.
> 
> My problem is. Im trying to import data from a .csv file
> using the
> read.table command. The .csv file header starts on row 2
> but is contained in
> column 1, i have 600 data files and for future ease would
> rather not edit
> each file seperatly. The data starts on row three and I
> only need the first
> 381 data points.
> 
> The R error message using the code iv got so far is
> 
> Error in read.table(file("s1-2c83.csv"), header = FALSE,
> sep = ",", quote =
> "",  : 
>   more columns than column names
> 
> The code I have so far is
> 
> framename<-read.table(file ("s1-2c83.csv"),
> header = FALSE, # FLASE indicates headers are not included
> in input file
> sep = ",",    # must have "," otherwise errors in
> table
> quote = "",
> dec = ".",
> row.names = 1, # must = 1 or extra column of row numbering
> is entered
> col.names =
> ("Nr2sec,Cnt1X,Cnt1Y,Cnt2X,Cnt2Y,sec100,hour"),
> as.is = FALSE,
> na.strings = "NA",
> colClasses = NULL,
> nrows = 381, # rows to stop data.table recording (not input
> file row
> number!)
> skip = 2,    # number of rows to skp before
> reading data from input file
> strip.white = FALSE,
> comment.char = "")
> 
> write.csv(framename, file = "s1-2c83-ok.csv")
> 
> If I delete the line col.names, Iv manged to get the data
> read and saved to
> a new .csv file but cannot work out how to get the column
> headers renamed.
> The read.table (framename) displays the headers as v1,v2,v3
> etc, this is
> what i cant change. Also it has the first column without a
> header (i think
> its the row number) which I dont want in the output file
> 
> The read data file example s1-2c83.csv
> 
> 1:Samplerate = 2 samps/sec   
>            
>         
> 2:     
> Nr   Cnt1X   Cnt1Y   Cnt2X   Cnt2Y 
> sec100  hour       
>            
>     
> 3: 1    53   
> 84    43   
> 2    22    12
> 4: 2    90   
> 155    74   
> 0    72    12
> 5: 3    90   
> 155    74   
> 0    121    12
> 
> Any help will be greatly appreciated after the 5hrs Iv
> spent already on this
> problem.
> 
> Many thanks in advance
> 
>  Adam
> 
> 
> 
> -- 
> View this message in context: http://n4.nabble.com/Column-naming-issues-using-read-table-tp978241p978241.html
> Sent from the R help mailing list archive at Nabble.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.
> 


      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/




More information about the R-help mailing list