循环遍历所有文件:" missForest"包装在R

时间:2015-12-31 18:24:23

标签: r loops csv

搜索问题的结果: 但是,我可能无法正确应用解决方案。

  1. Which is the best method to apply a script repetitively to n .csv files in R?
  2. Looping through all files in directory in R, applying multiple commands
  3. Looping through all files in a directory in R
  4. 我的案例 我有一个包含数百个csv文件的文件夹。我需要运行" missForest"在R中打包以包含缺失值。 如果我逐个运行文件包,我必须执行以下操作:

    ##change the file name
    G1344108 <- read.csv(file.choose(), header = TRUE)
    
    ##run missForest with default settings
    G1344108.Forest <- missForest(G1344108, verbose = TRUE, maxiter = 5)
    
    ##save the imputed data matrix using filename$ximp 
    write.csv(G1344108.Forest$ximp, file = 'G1344108_output.csv')
    

    问题 如何循环浏览文件夹中的文件?

    我以这种方式运行代码:

       all.files <- list.files()
       my.files <- grep(".*csv", all_files, value=T)
                for(i in my.files){
        # do your operations here
        G1344108.Forest <- missForest(G1344108, verbose = TRUE, maxiter = 5)
        # save
        output.filename <- gsub("(.*?).csv", "\\1.csv", i)
        write.table(G1344108.Forest$ximp, output.filename)
    }
    

    它重现了我以下错误:

    Error in grep(".*csv", all_files, value = T) : 
      object 'all_files' not found
    Error: object 'my.files' not found
    

0 个答案:

没有答案