且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

导入文本文件时跳过空文件

更新时间:2022-03-02 10:20:06

对于引入显式错误处理的另一种方法,请考虑使用tryCatch处理在您的read.table中可能发生的其他不良情况.

For a different approach that introduces explicit error handling, think about a tryCatch to handle anything else bad that might happen in your read.table.

for (f in files) {
    data <- tryCatch({
        if (file.size(f) > 0){
        read.table(f, header = F, sep=",")
           }
        }, error = function(err) {
            # error handler picks up where error was generated
            print(paste("Read.table didn't work!:  ",err))
        })
    data$species <- strsplit(f, split = "c.txt") 
    DF <- rbind(DF, data)
}