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

时间:2015-10-16 16:40:36

标签: r for-loop read.table

我有一个包含大约700个文本文件的文件夹,我想导入并添加一列。我已经使用以下代码了解了如何执行此操作:

files = list.files(pattern = "*c.txt")
DF <- NULL
for (f in files) {
  data <- read.table(f, header = F, sep=",")
  data$species <- strsplit(f, split = "c.txt") <-- (column name is filename)
  DF <- rbind(DF, data)
}
write.xlsx(DF,"B:/trends.xlsx")

问题是,大约有100个文件是空的。所以代码停在第一个空文件,我收到此错误消息:

Error in read.table(f, header = F, sep = ",") : 
  no lines available in input

有没有办法跳过这些空文件?

2 个答案:

答案 0 :(得分:5)

您可以通过检查file.size(some_file) > 0

来跳过空文件
files <- list.files("~/tmp/tmpdir", pattern = "*.csv")
##
df_list <- lapply(files, function(x) {
    if (!file.size(x) == 0) {
        read.csv(x)
    }
})
##
R> dim(do.call("rbind", df_list))
#[1] 50  2

这会跳过10个空的文件,并读取其他10个不是的文件。

数据:

for (i in 1:10) {
    df <- data.frame(x = 1:5, y = 6:10)
    write.csv(df, sprintf("~/tmp/tmpdir/file%i.csv", i), row.names = FALSE)
    ## empty file
    system(sprintf("touch ~/tmp/tmpdir/emptyfile%i.csv", i))
}

答案 1 :(得分:2)

对于引入显式错误处理的其他方法,请考虑使用tryCatch来处理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)
}
相关问题