你在寻找这样的东西吗?
do.call(rbind, lapply(list.files(path=".", pattern="AM-25"), read.table, header=TRUE, sep=","))
这将从包含字符“AM-25”的 csv 文件中读取的矩阵重新绑定在一起。的参数read.table
可能会有所不同,具体取决于您的 csv 文件。
编辑
我希望这适用于您不知道目录中所有可能的文件名的五个字母前缀的情况:
##Get all different first five letter strings for all cvs files in directory "."
file.prefixes <- unique(sapply(list.files(path=".", pattern="*.csv"), substr, 1,5))
##Group all matching file names according to file.prefixes into a list
file.list <- lapply(file.prefixes, function(x)list.files(pattern=paste("^",x,".*.csv",sep=""), path="."))
names(file.list) <- file.prefixes ##just for convenience
##parse all csv files in file.list, create a list of lists containing all tables for each prefix
tables <- lapply(file.list, function(filenames)lapply(filenames, function(file)read.table(file, header=TRUE)))
##for each prefix, rbind the tables. Result is a list of length being length(file.prefixes)
## each containing a matrix with the combined data parsed from the files that match the prefix
joined.tables <- lapply(tables, function(t)do.call(rbind, t))
##Save tables to files
for (prefix in names(joined.tables))write.table(joined.tables[[prefix]], paste(prefix, ".csv", sep=""))