目录中多个文件的 Rbind 正在复制第一个文件的条目
Rbind on multiple files in directory is duplicating the entries of first file
程序浏览一个文件夹中的所有文件,处理它们并将它们绑定到一个文件中:
files=list.files(path="path", recursive=T, pattern='.xlsx')
for(i in 1:length(files))
{
#some process goes which generates qs_30 for each file as the loop is run
if (!exists("dataset")){
dataset <- qs_30
}
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_xts <-qs_30
dataset<-rbind(dataset, temp_xts)
rm(temp_xts)
}
}
写入最终数据集文件时,它会复制第一个 qs_30 的条目。请帮忙调试这个。
出现您的问题是因为数据集是在第一个 if 之后创建的,然后进入第二个 if,因此执行了 rbind
两次
您有两个选择:
交换两者的顺序 if
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_xts <-qs_30
dataset<-rbind(dataset, temp_xts)
rm(temp_xts)
}
if (!exists("dataset")){
dataset <- qs_30
}
或者做一个else而不是if
if (!exists("dataset")){
dataset <- qs_30
} else {
temp_xts <-qs_30
dataset<-rbind(dataset, temp_xts)
rm(temp_xts)
}
程序浏览一个文件夹中的所有文件,处理它们并将它们绑定到一个文件中:
files=list.files(path="path", recursive=T, pattern='.xlsx')
for(i in 1:length(files))
{
#some process goes which generates qs_30 for each file as the loop is run
if (!exists("dataset")){
dataset <- qs_30
}
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_xts <-qs_30
dataset<-rbind(dataset, temp_xts)
rm(temp_xts)
}
}
写入最终数据集文件时,它会复制第一个 qs_30 的条目。请帮忙调试这个。
出现您的问题是因为数据集是在第一个 if 之后创建的,然后进入第二个 if,因此执行了 rbind
两次
您有两个选择:
交换两者的顺序 if
# if the merged dataset does exist, append to it
if (exists("dataset")){
temp_xts <-qs_30
dataset<-rbind(dataset, temp_xts)
rm(temp_xts)
}
if (!exists("dataset")){
dataset <- qs_30
}
或者做一个else而不是if
if (!exists("dataset")){
dataset <- qs_30
} else {
temp_xts <-qs_30
dataset<-rbind(dataset, temp_xts)
rm(temp_xts)
}