I have a folder with 700 text files that I want to import and add a column. I figured out how to do this using the following code:
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")
The problem is that there are about 100 files that are empty. so the code stops in the first empty file, and I get this error message:
Error in read.table (f, header = F, sep = ","): there are no lines in the input line
Is there any way to skip these empty files?
Thanks!
source
share