Any ideas why read.table complains about not correct number of elements in line while readLine/strsplit indicate that all lines have the same number of elements ? R> tbl <- read.table('tmp', header = T, sep = '\t')Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 32 did not have 27 elements> lines <- readLines('tmp') > v <- 1:length(lines) > i <- 0; for (line in lines) { i <- i + 1; v[i] <- length(strsplit(line,'\t')[[1]]) }> v[1] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 [26] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 [51] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27> sum((v - v[1])^2)[1] 0> length(strsplit(lines[32], '\t')[[1]])[1] 27
ryszard.czerminski at pharma.novartis.com writes:> Any ideas why read.table complains about not correct number of elements in > line > while readLine/strsplit indicate that all lines have the same number of > elements ? > > R > > > > tbl <- read.table('tmp', header = T, sep = '\t') > Error in scan(file = file, what = what, sep = sep, quote = quote, dec = > dec, : > line 32 did not have 27 elements > > lines <- readLines('tmp') > > v <- 1:length(lines) > > i <- 0; for (line in lines) { i <- i + 1; v[i] <- length(strsplit(line, > '\t')[[1]]) } > > v > [1] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 > 27 27 > [26] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 > 27 27 > [51] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 > > sum((v - v[1])^2) > [1] 0 > > length(strsplit(lines[32], '\t')[[1]]) > [1] 27Try count.fields to see what line is giving problems. Watch especially for comment characters (default is '#') and quote characters. My good friends in Biological Sciences (including my spouse and one of my children) have a habit of incorporating 3' and 5' in descriptive text and that always trips me up.
On Tue, 3 Feb 2004 ryszard.czerminski at pharma.novartis.com wrote:> Any ideas why read.table complains about not correct number of elements in > line > while readLine/strsplit indicate that all lines have the same number of > elements ?That is what count.fields is for. Setting fill=TRUE in read.table can help detection, too. One guess is that you have a comment character on that line, but one thing you did not show us is the appropriate lines of the file.> > tbl <- read.table('tmp', header = T, sep = '\t') > Error in scan(file = file, what = what, sep = sep, quote = quote, dec = > dec, : > line 32 did not have 27 elements > > lines <- readLines('tmp') > > v <- 1:length(lines) > > i <- 0; for (line in lines) { i <- i + 1; v[i] <- length(strsplit(line, > '\t')[[1]]) } > > v > [1] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 > 27 27 > [26] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 > 27 27 > [51] 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 > > sum((v - v[1])^2) > [1] 0 > > length(strsplit(lines[32], '\t')[[1]]) > [1] 27 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595