2016-08-17 59 views
0

如果有幫助,我在MacBook Pro上的操作系統船長巨巖運行v 3.3.1 ...錯誤數據的讀取和文件夾合併成一個數據幀

我想類似的文件夾中閱讀數據文件。我檢查目錄和文件,他們應該是:

list.files('../data/') 
[1] "B101.txt" "B101p2.txt" "B116.txt" "B6.txt"  "B65.txt" "B67.txt" "B67p2.txt" 
[8] "B70.txt" "B71.txt" "B71p2.txt" "B95.txt" "B95p2.txt" "B96.txt" "B96p2.txt" 
[15] "B98.txt" "B98p2.txt" "B99.txt" "B99p2.txt" 

以下是我的代碼和錯誤:

a = ldply(
    .data = list.files(
     path = '../data/' 
      ) 
    , .fun = function(x){ 
     to_return = read.table(
      file = x 
      , skip = 20 
      , sep = '\t' 
      , fill = TRUE 
       ) 
     return(to_return) 
      } 
    , .progress = 'text' 
) 

Error in file(file, "rt") : cannot open the connection 
In addition: Warning message: 
In file(file, "rt") : 
    cannot open file 'B101.txt': No such file or directory 

我不知道是什麼問題,爲那些所有搜索錯誤提示修復目錄。我還檢查數據文件,並可以讀取使用一個單獨的文件:

read.table('../data/B101.txt', skip = 20, sep = '\t', fill=TRUE) 

可能有人請幫我解決的整個文件夾讀取問題。我試圖用少量的文件來理清腳本,但是需要它來運行一個更大的數字,所以逐個閱讀它們是不現實的。謝謝。

回答

1

默認情況下,list.files返回只有文件名本身,不包括領先的(相對或絕對)路徑(如果有的話)。當處理可能位於另一個目錄中的文件時,需要包含full.names = TRUE

a = ldply(
    .data = list.files(
     path = '../data/', 
     full.names = TRUE 
      ) 
    , .fun = function(x){ 
     to_return = read.table(
      file = x 
      , skip = 20 
      , sep = '\t' 
      , fill = TRUE 
       ) 
     return(to_return) 
      } 
    , .progress = 'text' 
)