1

我正在尝试读取多个文件,然后将它们组合成一个数据框。我正在使用的代码如下:

library(plyr)
mydata = ldply(list.files(path="Data load for stations/data/Predicted",pattern = "txt"), function(filename) {
  dum = read.table(filename,skip=5, header=F, sep=" ")

  #If you want to add the filename as well on the column

  dum$filename = filename
  return(dum)
})

我得到的错误如下:

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

数据文件可在https://www.dropbox.com/sh/827kmkrwd0irehk/BFbftkks42上找到

非常感谢任何帮助。

4

2 回答 2

5

或者,您可以full.names在中使用参数list.files

list.files(path="Data load for stations/data/Predicted",
           pattern = "txt", full.names=TRUE)

它将自动在文件名之前添加完整路径。

于 2013-05-28T07:46:27.097 回答
4

试试下面的代码:

library(plyr)
path <- "Data load for stations/data/Predicted/"
filenames <- paste0(path, list.files(path, pattern = "txt"))
mydata = ldply(filenames, function(filename) {
  dum = read.table(filename,skip=5, header=F, sep=" ")

  #If you want to add the filename as well on the column

  dum$filename = filename
  return(dum)
})

我认为正在发生的事情是你正在生成一个相对于路径的文件列表list.files,然后要求read.table在没有路径的其余部分的情况下获取文件名......

于 2013-05-28T06:14:20.090 回答