2016-08-17 56 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' 
)