简体   繁体   English

R中的Read.table错误

[英]Read.table error in R

I'm using R to read a text file and then subsequently manipulate it. 我正在使用R来读取文本文件然后进行操作。 The input file has 22 columns. 输入文件有22列。 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. 我是R的新手,我非常感谢你的帮助。 I've tried to make sure this isn't a repost, but if it is, please do link me to the original. 我试图确保这不是转发,但如果是,请将我链接到原始版本。

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.txt'并存储在您的工作目录中,这应该工作:

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 假设您的文本文件不是很大,一种解决方法是在RStudio中打开一个新的R脚本然后输入

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. 然后将文本文件中的所有文本复制并粘贴到上面一行中的""之间,不要对换行符或格式进行任何更改,然后选择all并将其发送到控制台。

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. 如果这不实用或不可能,请在您的问题中发布指向您的文本文件的链接(如下所示: http//temp-share.com/show/dPf3a6oHW在45天后自动删除),以便其他人可以查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM