我正在尝试读取文件列表并将它们附加到包含所有记录的新文件中.我不打算改变原始文件中的任何内容.我尝试了几种方法.
方法1:此方法创建一个新文件,但在每次迭代时,再次添加先前的文件.因为我以递归方式绑定数据帧.
files <- list.files(pattern = "\\.csv$")
#temparary data frame to load the contents on the current file
temp_df <- data.frame(ModelName = character(), Object = character(),stringsAsFactors = F)
#reading each file within the range and append them to create one file
for (i in 1:length(files)){
#read the file
currentFile = read.csv(files[i])
#Append the current file
temp_df = rbind(temp_df, currentFile)
}
#writing the appended file
write.csv(temp_df,"Models_appended.csv",row.names = F,quote = F)
Run Code Online (Sandbox Code Playgroud)
方法2:我从Rbloggers获得了这个方法.此方法不会写入新文件,而是继续修改原始文件.
multmerge = function(){
filenames= list.files(pattern = "\\.csv$")
datalist = lapply(filenames, function(x){read.csv(file=x,header=T)})
Reduce(function(x,y) {merge(x,y)}, temp_df)
}
Run Code Online (Sandbox Code Playgroud)
有人可以就如何实现我的目标向我提出建议吗?
您可以使用它将所有内容加载到一个数据集中.
dataset <- do.call("rbind", lapply(file.list, FUN = function(file) {
read.table(file, header=TRUE, sep="\t")
}))
Run Code Online (Sandbox Code Playgroud)
然后只需保存write.csv.
它看起来像这样:
files <- list.files(pattern = "\\.csv$")
DF <- read.csv(files[1])
#reading each file within the range and append them to create one file
for (f in files[-1]){
df <- read.csv(f) # read the file
DF <- rbind(DF, df) # append the current file
}
#writing the appended file
write.csv(DF, "Models_appended.csv", row.names=FALSE, quote=FALSE)
Run Code Online (Sandbox Code Playgroud)
或短:
files <- list.files(pattern = "\\.csv$")
DF <- read.csv(files[1])
for (f in files[-1]) DF <- rbind(DF, read.csv(f))
write.csv(DF, "Models_appended.csv", row.names=FALSE, quote=FALSE)
Run Code Online (Sandbox Code Playgroud)
或者您可以继续在 R 中使用 shell 命令:
system2("cat", args = "*.csv", stdout = "appendedfiles.csv")
Run Code Online (Sandbox Code Playgroud)
这适用于基于 UNIX 的系统;我不确定你会为 windows 做什么。