user2211355
user2211355

Reputation: 57

Read.table error in R

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.

Upvotes: 0

Views: 5295

Answers (1)

Ben
Ben

Reputation: 42283

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.

Upvotes: 2

Related Questions