3

我有四个文件夹,其中包含同名的文本文件(制表符分隔),我想将所有这些文本文件导入 data.frame。例如:

TopFolder = "G:\\University" 
SubFolder = list.files(TopFolder)
#find the name of the folders in the current directory
DateTime = rbind(read.table(paste(TopFolder,SubFolder[1],"Data.txt",sep = "\\"),sep="\t"),
                read.table(paste(TopFolder,SubFolder[2],"Data.txt",sep = "\\"),sep="\t"),
                 read.table(paste(TopFolder,SubFolder[3],"Data.txt",sep = "\\"),sep="\t"),
                 read.table(paste(TopFolder,SubFolder[4],"Data.txt",sep = "\\"),sep="\t"))

这个例子工作得很好,虽然我希望使用循环或其他函数来生成这个变量,而不必单独导入所有文件。有没有人有什么建议?

4

1 回答 1

4

这个怎么样?

lf = list.files(path = "G:\\University", pattern = "Data.txt", 
                full.names = TRUE, recursive = TRUE, include.dirs = TRUE)

library(plyr)
DateTime = ldply(lf, read.table, sep = "\t")
于 2012-05-21T08:44:36.897 回答