read.csv与read.table [英] read.csv vs. read.table

查看:216
本文介绍了read.csv与read.table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在几个例子中看到,虽然 read.table()不能读取制表符分隔的文件(例如微阵列的注解表)以下错误:

pre $ 扫描中的错误(file,what,nmax,sep,dec,quote,skip,nlines,na.strings ,:
line xxx没有yyy​​元素

读取。 csv()完全可以在同一个文件中完成,没有错误。我认为 read.csv()的速度也高于 read.table()



更多: read.table()正在读取我的一个文件非常疯狂,它在读取第100行的时候会出现这个错误,但是当我在同一个文件的头部之后复制并粘贴第90行到第110行时,它仍然会使第100 + 21行的错误(在开头复制了新的行)如果这行有什么问题,为什么不在开始时读取粘贴的行时报错?我确认 read.csv() r您可以使用相同的文件,而不会出错。



您是否知道为什么 read.table()读取 read.csv()相同的文件?还有没有任何理由在任何情况下使用 read.table()

解决方案

read.csv read.table ;如果不能正确地复制 read.csv 的行为,通过给 read.table 提供正确的参数,我会感到非常惊讶$ C>。然而,其中一些参数(如处理引号或注释字符的方式)可能会改变函数的速度和行为。



特别是,是 read.csv 的完整定义:

  function(file,header = TRUE,sep =,,quote =\,dec =。,
fill = TRUE,comment.char =,...) {
read.table(file = file,header = header,sep = sep,quote = quote,$ b $ dec = dec,fill = fill,comment.char = comment.char,...)





$ p

如上所述,它只是 read.table

由于@Chase在下面的注释中声明了 read.table()的帮助页面, 在详情中表示: $ b


.csv和read.csv2与read.table相同,除了默认值之外,它们用于读取'逗号分隔值文件(的.csv)或(read.csv2)在使用逗号作为小数点和分号作为字段分隔符国家所使用的变体。



I have seen in several cases that while read.table() is not able to read a tab delimited file (for example the annotation table of a microarray) returning the following error:

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

read.csv() works perfectly on the same file with no errors. I think also the speed of read.csv() is also higher than read.table().

Even more: read.table() is doing very crazy reading a file of me. It makes this error while reading line 100, but when I copy and paste lines 90 to 110 just after the head of the same file, it still makes error of line 100+21 (new lines copied at the beginning). If there is any problem with that line, why doesn't it report that error while reading the pasted line at the beginning? I confirm that read.csv() reads the same file with no error.

Do you have any idea of why read.table() is unable to read the same files that read.csv() works on it? Also is there any reason to use read.table() in any cases?

解决方案

read.csv is a fairly thin wrapper around read.table; I would be quite surprised if you couldn't exactly replicate the behaviour of read.csv by supplying the correct arguments to read.table. However, some of those arguments (such as the way that quotation marks or comment characters are handled) could well change the speed and behaviour of the function.

In particular, this is the full definition of read.csv:

function (file, header = TRUE, sep = ",", quote = "\"", dec = ".", 
    fill = TRUE, comment.char = "", ...) {
     read.table(file = file, header = header, sep = sep, quote = quote, 
        dec = dec, fill = fill, comment.char = comment.char, ...)
}

so as stated it's just read.table with a particular set of options.

As @Chase states in the comments below, the help page for read.table() says just as much under Details:

read.csv and read.csv2 are identical to read.table except for the defaults. They are intended for reading ‘comma separated value’ files (‘.csv’) or (read.csv2) the variant used in countries that use a comma as decimal point and a semicolon as field separator.

这篇关于read.csv与read.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆