0

I'm using R to read a text file and then subsequently manipulate it. The input file has 22 columns. This is what the first column looks like :

NAME    LENGTH  A   C   D   E   F   G   H   I   K   L   M   N   P   Q   R   S   T   V   W   Y

I am currently using:

read.table("filename", stringsAsFactors=FALSE) 

to input the file. When I run the same, I get this warning:

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

Not sure where I am going wrong. I'm new to R and I would really appreciate your help. I've tried to make sure this isn't a repost, but if it is, please do link me to the original.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
user2211355
  • 57
  • 1
  • 3
  • 7
  • 2
    Try `header = TRUE` and show us the first few lines of the file. – Roman Luštrik Apr 01 '13 at 11:05
  • Or maybe/too use `sep=SEPATATOR`, where `SEPARATOR` is the token that separates inputs (like comma for `.csv` files) – Rcoster Apr 01 '13 at 11:41
  • @RomanLuštrik Tried header=TRUE, but it is displaying the same error and not printing anything. Here are the first few lines of the file from the txt file itself if it helps : NAME LENGTH A C D E F G H I K L M N P Q R S T V W Y ape:APE_0001 242 15 0 1 12 10 18 2 27 9 43 7 2 8 3 5 25 15 24 3 12 ape:APE_0002 113 7 1 6 6 1 12 3 4 10 16 4 2 4 0 10 3 5 9 4 5 ape:APE_0004 305 24 2 5 8 9 25 4 36 12 43 8 11 14 2 12 20 21 27 9 12 All rows have 22 columns (verified with count.fields) so this error of lines not having 23 elements shouldn't arise at all! Do tags not work in comments!? – user2211355 Apr 01 '13 at 12:30
  • 1
    That colon ":" in there may be fouling up your apparent number of elements. Try using the `fill=TRUE` argument to `read.table` and see what you get. – Carl Witthoft Apr 01 '13 at 12:47
  • 1
    @user2211355 Edit your question and add the sample data there. – Roland Apr 01 '13 at 12:50

1 Answers1

2

Assuming the text file looks like this:

NAME LENGTH A C D E F G H I K L M N P Q R S T V W Y 
ape:APE_0001 242 15 0 1 12 10 18 2 27 9 43 7 2 8 3 5 25 15 24 3 12 
ape:APE_0002 113 7 1 6 6 1 12 3 4 10 16 4 2 4 0 10 3 5 9 4 5 
ape:APE_0004 305 24 2 5 8 9 25 4 36 12 43 8 11 14 2 12 20 21 27 9 12

and is called 'dat.txt' and stored in your working directory, this should just work:

dat <- read.table("dat.txt", stringsAsFactors=FALSE, header=TRUE)
# to give:
dat
          NAME LENGTH  A C D  E  F  G H  I  K  L M  N  P Q  R  S  T  V W  Y
1 ape:APE_0001    242 15 0 1 12 10 18 2 27  9 43 7  2  8 3  5 25 15 24 3 12
2 ape:APE_0002    113  7 1 6  6  1 12 3  4 10 16 4  2  4 0 10  3  5  9 4  5
3 ape:APE_0004    305 24 2 5  8  9 25 4 36 12 43 8 11 14 2 12 20 21 27 9 12

Since that doesn't appear to be working for you, there might be something odd and invisible going on in your text file, hidden characters, etc.

Assuming your text file isn't enormous, one workaround would be to open a new R script in RStudio then type in

dat <- read.table(stringsAsFactors=FALSE, header=TRUE, text = "")

And then copy and paste all the text in your text file between the "" in the line above, without any changes to line breaks or formatting, and then select all and send it to the console.

For the example in your comment that would look like this:

dat <- read.table(header=TRUE, stringsAsFactors=FALSE, text = "NAME LENGTH A C D E F G H I K L M N P Q R S T V W Y 
ape:APE_0001 242 15 0 1 12 10 18 2 27 9 43 7 2 8 3 5 25 15 24 3 12 
ape:APE_0002 113 7 1 6 6 1 12 3 4 10 16 4 2 4 0 10 3 5 9 4 5 
ape:APE_0004 305 24 2 5 8 9 25 4 36 12 43 8 11 14 2 12 20 21 27 9 12")

If that's not practical or possible, post a link to your text file in your question (like this: http://temp-share.com/show/dPf3a6oHW deleted automatically after 45 Days) so others can have a look.

Ben
  • 41,615
  • 18
  • 132
  • 227