0

I have a txt file with only 59.6KB. and I write my scripts as I did thousand times

cancer<-read.table("cancerGenes.txt", row.names=1,header=T)

And it gives me the error message:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 1 did not have 28 elements

what went wrong??!!

jlhoward
  • 58,004
  • 7
  • 97
  • 140
lxcfuji
  • 329
  • 1
  • 4
  • 15
  • Does your file have any uncommon character encoding? I have encountered this error in such cases before. – asb Mar 22 '14 at 18:46
  • like what kinds of uncommon character? how to fix it? – lxcfuji Mar 22 '14 at 18:48
  • 1
    Perhaps post the first 3 rows of your data set, or a set of fake data in the identical format. – Mark Miller Mar 22 '14 at 18:50
  • never mind, I converted into csv file, then it works! I dont know whats going on, but thank you two – lxcfuji Mar 22 '14 at 18:58
  • Actually the default separator is any number of spaces, a so-called "whitespace" separator. `> read.table(text="a b") # V1 V2 1 a b`. Tab-characters are also seen as whitespace unless sep ='\t' – IRTFM Mar 22 '14 at 21:20

1 Answers1

0

Try using the table(count.fields( ... ) strategy:

table(count.fields("yourpath/filename.txt", sep="", stringsAsFactors=FALSE, 
      allowEscapes = TRUE, quote="", comment.char=""))

Then add back values to 'quote' or 'comment.char' if needed. The default values for 'quote' are the two character string "'\"" and you may want to first try quote="\"" to keep names with single quotes (like "O'Malley") from causing problems , and the default for comment.char is the octothorpe, "#". Mismatched quotes are common in unclean data files and you may need to do some further investigation to print out lines with abnormal numbers of elements.

Posting a link to a location where we can examine the file is the next step for you.

IRTFM
  • 258,963
  • 21
  • 364
  • 487