Skip to content

Problem with read.table()

3 messages · Kristian Skrede Gleditsch, Brian Ripley

#
Dear all,

I have encountered a strange problem with read.table(). When I try to 
read a tab delimited file I get an error message for line 260 not being 
equal to 14 (see below).

Using count.fields() suggests that a number of lines have length not 
equal to 14, but not 260.

Looking at the actual file, however, I cannot see anything wrong with 
any lines. They all seem to have length 14, there are no double tabs 
etc., and the file reads correctly in other programs. Does anyone have 
any suggestions as to what this might stem from?

I have placed a copy of the file at 
http://dss.ucsd.edu/~kgledits/archigos_v.1.9.asc

regards,
Kristian Skrede Gleditsch


 > archigos1.9 <- read.table("c:/work/work12/archigos/archigos_v.1.9.asc",
+     sep="\t",header=T,as.is=T,row.names=NULL)
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = 
dec,  :
         line 260 did not have 14 elements
 > a <- count.fields("c:/work/work12/archigos/archigos_v.1.9.asc",sep="\t")
 > a <- data.frame(c(1:length(a)),a)
 > a[a[,2]!=14,]
      c.1.length.a..  a
150             150 10
313             313 10
424             424 10
1189           1189  5
1510           1510 10
1514           1514 10
1590           1590  5
1600           1600 10
1612           1612 10
1618           1618 10
1619           1619 10
1709           1709 10
1722           1722 10
1981           1981 10
1985           1985 10
2112           2112 10
2178           2178 10
2208           2208 10
2224           2224 10
2530           2530  5
2536           2536  5
2573           2573  5
2928           2928  5
#
On Thu, 21 Jul 2005, Kristian Skrede Gleditsch wrote:

            
Most `strange problems' are user error, so please try not to blame your 
tools.
Yes, but not line 260 in that file, but line 260 as read by scan().

Think about quotes ... it works for me with quote="", and the quote on ca 
line 150 is causing you to get some very large fields with embedded new 
lines and tabs.

BTW, there is a 'R Data Import/Export' manual which goes through 
step-by-step the assumptions you make when using read.table with various 
options.  Do read it now.

  
    
#
Thanks to all who responded to my earlier message. The problem lies in 
that apostrophes (i.e., ') in some of the text fields are read as quotes.

The file can be read without problems setting quotes="" in read.table.

Incidently, read.delim() also works, even without setting quotes="" 
explicitly.

best regards,

Kristian Skrede Gleditsch
Department of Political Science, UCSD
(On leave, University of Essex, 2005-6)
Tel: +44 1206 872499, Fax: +44 1206 873234
Email: kgleditsch at ucsd.edu or ksg at essex.ac.uk
http://weber.ucsd.edu/~kgledits/
Kristian Skrede Gleditsch wrote: