Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中多次执行for循环_R_For Loop - Fatal编程技术网

在R中多次执行for循环

在R中多次执行for循环,r,for-loop,R,For Loop,我有大约631个目录,每个目录中有20个文件。我想为所有目录执行下面的代码,以便它可以在每次迭代中更新dir_1到dir_2,直到dir_631。我试过双倍跳环,但没能成功。提前谢谢 library(TeachingDemos) txtStart("command_split_1000/dir_1.txt") files <- list.files(path="data/split_1000/dir_1", pattern="x*", full.names=TRUE) total.co

我有大约631个目录,每个目录中有20个文件。我想为所有目录执行下面的代码,以便它可以在每次迭代中更新dir_1到dir_2,直到dir_631。我试过双倍跳环,但没能成功。提前谢谢

library(TeachingDemos)

txtStart("command_split_1000/dir_1.txt")
files <- list.files(path="data/split_1000/dir_1", pattern="x*", full.names=TRUE)

total.coefs <- data.frame()

for (x in files) {
  message('Running: ', x)

  output <- tryCatch({
    ulfasQTL::findSqtl(x, geneObjectName = "gene_after_preprocess", snpFileCate = 1)
  }, print(x), error=function(e) {
    cat("ERROR :", conditionMessage(e), "\n")
  })

  total.coefs <- rbind(total.coefs, output)
  write.table(total.coefs, file = 'output_split_1000/dir_1', sep='\t')

}

txtStop()
库(教学演示)
txtStart(“命令分割\u 1000/dir\u 1.txt”)

文件考虑将
列表.files
循环嵌套在
列表.dirs
循环中。另外,避免在循环中使用
rbind
,因为这会导致内存中的复制过多(请参见Patrick Burns':圆圈2-成长对象)。而是使用
lappy
为循环外部的
rbind
构建数据帧列表

# RETRIEVE ALL NEEDED DIRECORIES
dirs <- list.dirs(path="data/split_1000")

for (d in dirs) {
  txtStart(paste0("command_split_1000/", basename(d), ".txt"))

  # RETRIEVE ALL FILES IN CURRENT DIRECTORY
  message('Directory: ', d)
  files <- list.files(path=d, pattern="x*", full.names=TRUE)

  # LIST OF DATA FRAMES
  df_list <- lapply(files, function(x) {
      message('--Running: ', x)

      output <- tryCatch({
         ulfasQTL::findSqtl(x, geneObjectName = "gene_after_preprocess", snpFileCate = 1)
      }, print(x), error=function(e) {
         cat("ERROR :", conditionMessage(e), "\n")
      })
  })

  # ROW BIND ALL NON-NULL DF ELEMENTS
  df_list <- Filter(NROW, df_list) 
  total.coefs <- do.call(rbind, df_list)

  # SAVE OUTPUT WITH BASE NAME OF CURRENT DIRECTORY
  out_path <- paste0('output_split_1000/', basename(d), '.txt')
  write.table(total.coefs, file = out_path, sep='\t')

  txtStop()

  # RELEASE RESOURCES
  rm(df_list, files, total.coefs)
  gc()
}
#检索所有需要的目录

目录还是文件?您的帖子与代码不一致。注释R是否也有
列表.dirs
?现在发生了什么-只处理一组目录文件?每个目录中有20个文件,我有631个目录,我想为所有目录中存在的所有文件运行此代码您可以为目录中的目录创建一个嵌套循环
为文件中的文件`使用目录列表构建路径的地方非常感谢您的努力,目前我面临两个问题,(1)它没有在txtStart中更新目录(“command_split_1000/dir_1.txt”),也许我们需要把它放在循环中。(2) 脚本在处理少量文件后停止,可能是由于内存问题,在这种情况下,可能是我们需要放置垃圾收集器(gc()),并需要在完成特定目录的迭代后重新启动R。是的,将
txtStart
txtStop
移动到循环内部。脚本在处理少量文件后停止。。。没有错误?撞车?总是在同一个目录上吗?检查文本文件。正在处理的这些文件有多大?是的,R崩溃,系统重新启动。我的原始文件是19 gb,包含12617361行,因此我根据1000行对主文件进行子集,并生成12620个文件。R终端崩溃还是RStudio崩溃?对于这些大批量作业,请尝试不要在RStudio中运行,而是在终端中没有GUI的命令行中运行:
Rscript”/path/to/my/R/code.R“
。我正在ubntu计算机上的R终端上运行。最后给出:ERROR:cannotallocatevector大小为249.7mb,这就是为什么我在想,在完成一个目录的迭代之后,可能需要重新启动R会话。就像我在完成一个目录的迭代之后逐个运行并关闭R会话一样,它工作得非常好。