如何使用fread读取R中的gz文件?
问题描述:
我在试图加快read.table步骤的Windows机器上。我的文件都是.gz。如何使用fread读取R中的gz文件?
x=paste("gzip -c ",filename,sep="")
phi_raw = fread(x)
Error in fread(x) :
无法理解错误。它对我来说有点太神秘了。
不像zx8754建议的那样重复:专门用于fread的上下文中。虽然fread dows没有原生的gzip支持,但这种模式应该可行。见http://www.molpopgen.org/coding/datatable.html
更新
每建议使用以下系统产生一个较长的错误消息 - 尽管我仍然坚持。
Error in fread(system(x)) :
'input' must be a single character string containing a file name, a command, full path to a file, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or the input data itself
In addition: Warning message:
running command 'gzip -c D:/x_.gz' had status 1
更新
与gunzip解运行如下指出:
Error in fread(system(x)) :
'input' must be a single character string containing a file name, a command, full path to a file, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or the input data itself
In addition: Warning message:
running command 'gunzip -c D:/XX_.gz' had status 127
注意不同的状态
答
我经常在Windows上用gzip用fread。它在不解压缩的情况下读入文件。我会尝试使用gzip命令添加-d选项。具体来说,在您的代码中,请尝试x=paste("gzip -dc ",filename,sep="")
。下面是我的机器上工作的可重复的例子:
df <- data.frame(x = 1:10, y = letters[1:10])
write.table(df, 'df.txt', row.names = F, quote = F, sep = '\t')
system("which gzip")
system("gzip df.txt")
data.table::fread("gzip -dc df.txt")
这里是我的sessionInfo()
。
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] rsconnect_0.4.3 tools_3.3.1 data.table_1.9.6 chron_2.3-47
我已经成功地使用gzip的Windows上不添加压缩文件同时使用Rtools(https://cran.r-project.org/bin/windows/Rtools/)和牌九(https://github.com/bmatzelle/gow/wiki)我的硬盘驱动器。如果我上面的重现示例不适用于您,请使用which gzip
和which gunzip
命令查看正在运行的确切.exe文件。如果它不是Rtools或Gow,可能尝试安装其中的一个,然后再次尝试可重现的示例。
https://github.com/Rdatatable/data.table/issues/717 – zx8754
[Decompress gz file using R]可能重复(http://stackoverflow.com/questions/5764499/decompress-gz-file-使用-r) – zx8754
不是重复的:在fread中特别使用。虽然fread dows没有原生的gzip支持,但这种模式应该可行。 – pythOnometrist