Hi, I'm a novice of R. I want to read the following table into R: names mpg cyl disp hp drat Mazda RX4 21.0 6 160.0 110 3.90 Mazda RX4 Wag 21.0 6 160.0 110 3.90 The command I used is:> test <- read.table(file.choose(),header=T)The result is: Error in read.table(file.choose(), header = T) : more columns than column names Can anybody tells me what is wrong?
-------- Original Message -------- Subject: [R] read table From: jiqiang yao <jiqiang123 at yahoo.com> To: r-help at stat.math.ethz.ch Date: 05.06.2007 18:17> Hi, > I'm a novice of R. > > I want to read the following table into R: > names mpg cyl disp hp drat > Mazda RX4 21.0 6 160.0 110 3.90 > Mazda RX4 Wag 21.0 6 160.0 110 3.90 > > The command I used is: > >> test <- read.table(file.choose(),header=T) >>Does: test <- read.table(file.choose(),header=T,sep="\t") work?
The default separator in read.table is white space, so the first line (header) has 6 elements, the second line has 7 elements, and the third has 8 elements. Either delete the spaces in the car names, or use sep="\t" (or whatever is appropriate). ?read.table explains this. Sarah On 6/5/07, jiqiang yao <jiqiang123 at yahoo.com> wrote:> Hi, > I'm a novice of R. > > I want to read the following table into R: > names mpg cyl disp hp drat > Mazda RX4 21.0 6 160.0 110 3.90 > Mazda RX4 Wag 21.0 6 160.0 110 3.90 > > The command I used is: > > test <- read.table(file.choose(),header=T) > > The result is: > Error in read.table(file.choose(), header = T) : > more columns than column names > > Can anybody tells me what is wrong? > > ___________________-- Sarah Goslee http://www.functionaldiversity.org
R considers Mazda RX4 Wag to be three columns. There fore you have 6 columns of data and only 6 names. Use " " like this "Mazda RX4" and Mazda Rx4 Wag" to indicate each is a single character value. --- jiqiang yao <jiqiang123 at yahoo.com> wrote:> Hi, > I'm a novice of R. > > I want to read the following table into R: > names mpg cyl disp hp drat > Mazda RX4 21.0 6 160.0 110 3.90 > Mazda RX4 Wag 21.0 6 160.0 110 3.90 > > The command I used is: > > test <- read.table(file.choose(),header=T) > > The result is: > Error in read.table(file.choose(), header = T) : > more columns than column names > > Can anybody tells me what is wrong? > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >