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

时间:2015-09-07 06:56:53

标签: r loops cbind

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

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

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

1 个答案:

答案 0 :(得分:1)

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.