我正在尝试读取多个文件,然后将它们合并为一个数据框。我使用的代码如下:
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
上找到数据文件非常感谢任何帮助。
答案 0 :(得分:5)
或者,您可以在full.names
中使用参数list.files
:
list.files(path="Data load for stations/data/Predicted",
pattern = "txt", full.names=TRUE)
它会自动添加文件名前的完整路径。
答案 1 :(得分: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
获取文件名而不使用其余的路径... < / p>