Cbind 通过进程生成的多个文件生成 #NA 数据

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

程序浏览一个文件夹中的所有文件,处理它们并将它们绑定到一个文件中:

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)
  }
}

最终数据集显示了一个很大的 table,其中包含很多 #NA。 qs_30 文件中没有#NA。请帮助在 qs_30 上应用 cbind,它在每次循环迭代时生成。 另外,有没有其他更有效的方法来绑定这些 qs_30.

您可以在一个列表中生成所有 data.frames,然后在最后一次性 cbind 它们:

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)

这还允许您分解生成 data.frames 和聚合它们的过程,以便在调试时更容易。