2016-06-09 237 views
1

我在試圖加快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 

注意不同的狀態

+0

https://github.com/Rdatatable/data.table/issues/717 – zx8754

+0

[Decompress gz file using R]可能重複(http://stackoverflow.com/questions/5764499/decompress-gz-file-使用-r) – zx8754

+0

不是重複的:在fread中特別使用。雖然fread dows沒有原生的gzip支持,但這種模式應該可行。 – pythOnometrist

回答

3

我經常在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 gzipwhich gunzip命令查看正在運行的確切.exe文件。如果它不是Rtools或Gow,可能嘗試安裝其中的一個,然後再次嘗試可重現的示例。