1

The program browse through all the files in a folder, process them and rbind them in a single 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)
  }
}

When the final dataset file is written it duplicates the entries of the first qs_30. Please help in debugging this.

gaurav kumar
  • 859
  • 2
  • 10
  • 24

1 Answers1

2

Your problem occurs because dataset is created after the first if an then enters the second if, thus doing the rbind twice

You have two options:

Exchange the order of the two 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
}

Or do an else instead of the if

if (!exists("dataset")){
    dataset <- qs_30
  } else {
    temp_xts <-qs_30
    dataset<-rbind(dataset, temp_xts)
    rm(temp_xts)
  }
R. Schifini
  • 9,085
  • 2
  • 26
  • 32