简体   繁体   English

R的read.table中的“和”之间有什么区别?

[英]what is the difference between '' and ' ' in read.table of R?

Here is my simple text (named test): 这是我的简单文字(命名为test):

  name      sex     age  height  
1 x1        F       18   162
2 x2        M       19   170
3 x3        M       21   178
4 x4        F       22   166
5 x5        F       23   165

>read.table('test', sep='') 

It's ok. 没关系。

>read.table('test', sep=' ')  

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

I want to know why. 我想知道为什么。

What is the difference between '' and ' ' in read.table of R? R的read.table中的''和“'有什么区别? Please tell me the reason. 请告诉我原因。

As explained in the documentation, the default value of '' means "any amount of whitespace" whereas your second choice ' ' means precisely one space which is in fact very different. 如文档中所述,默认值''表示“任意数量的空格”,而您的第二选择' ' 恰好表示一个实际上不同的空格

The wording (in help(read.table) ) is not ideal but gets the job done: 措辞(在help(read.table) )并不理想,但可以完成工作:

  sep: the field separator character. Values on each line of the file are separated by this character. If 'sep = ""' (the default for 'read.table') the separator is 'white space', that is one or more spaces, tabs, newlines or carriage returns. 

You want the default value, unless you know you have, say, a comma-delimited csv file. 您需要默认值,除非您知道有一个逗号分隔的csv文件。

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

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