在R中导入并rbind具有通用名称的多个csv文件 [英] Import and rbind multiple csv files with common name in R
问题描述
我有多个CSV文件,其名称中包含4个通用字符。
我想知道如何使用相同的通用字符来整理文件。例如, AM-25在3个csv文件的名称中是常见的,而 BA-35在另两个csv文件的名称中是常见的。
I have multiple CSV files with 4 common character in their names. I want to know how I can rbind the files with the same common character. For example, "AM-25" is common in name of 3 csv files and "BA-35" in the name of another 2.
这些文件就像
AM-25.myfiles.2000.csv,AM-25.myfiles.2001.csv,AM-25.myfiles.2002.csv,BA-35.myfiles.2000.csv,
BA-35 .myfiles.2001.csv,
我用它来读取所有文件:
The files are like this AM-25.myfiles.2000.csv, AM-25.myfiles.2001.csv ,AM-25.myfiles.2002.csv,BA-35.myfiles.2000.csv, BA-35.myfiles.2001.csv, I use this to read in all the files:
files <- list.files(path=".", pattern="xyz+.csv", all.files = FALSE,full.names=TRUE )
推荐答案
您是否正在寻找类似的东西?
Do you look for something like this?
do.call(rbind, lapply(list.files(path=".", pattern="AM-25"), read.table, header=TRUE, sep=","))
这将从您的csv文件中读取包含字符 AM-25的矩阵。
read.table
的参数可能不同,具体取决于您的csv文件。
This would rbind together the matrices read from your csv files which contain the characters "AM-25".
The arguments for read.table
could be different, depending on your csv files.
编辑
我希望这种方法适用于您不知道所有可能的五个字母前缀的情况目录中文件名的数量:
I hope this works for the case that you do not know all possible five-letter prefixes of filenames in your directory:
##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=""))
这篇关于在R中导入并rbind具有通用名称的多个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!