Search code examples
rloopscbind

Cbind on multiple file generated through a process generate #NA data


The program browse through all the files in a folder, process them and cbind 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<-cbind(dataset, temp_xts)
    rm(temp_xts)
  }
}

The final data set shows a large table with lot of #NA's. there is no #NA in qs_30 files. Please help in applying cbind on qs_30, which is generated each time the loop is iterated. Also, is there any other more efficient way to cbind these qs_30.


Solution

  • You can rather generate all the data.frames in a list and then cbind them in one shot at the end:

    files = list.files(path="path", recursive=T, pattern='.xlsx')
    lst   = lapply(files, function(x) {
        #some process goes which generates qs_30 for each file as the loop is run
        qs_30
    }
    
    do.call(cbind, lst)
    

    This allows also you to decompose the process of generating data.frames and aggregating them, easier in case of debugging.