Cheng
Cheng

Reputation: 193

how to skip blank files in read.table() in R

I want to read a lot of text files in a folder using read.table in R, but there are some blank file among those text files, errors coms when I using the following code.

filenames<-list.files("M:/files/test1",pattern=".txt");
datalist<-lapply(filenames,function(name){
  read.table(paste("M:/files/test1/",name,sep=""),head=FALSE,stringsAsFactors=FALSE,sep="\t")
})

Upvotes: 3

Views: 6101

Answers (2)

chen
chen

Reputation: 31

skip empty files

test the size of each file, and skip the file size of 0

for (file in list.files(,"*.txt")){
    if (file.size(file) == 0) next
    print(file)
}

Upvotes: 3

Thomas
Thomas

Reputation: 44565

The easiest way to do this is to add a simple error-catching mechanism using try:

datalist<-lapply(filenames,function(name){
  x <- try(read.table(paste("M:/files/test1/",name,sep=""),head=FALSE,stringsAsFactors=FALSE,sep="\t"))
  if(inherits(x, "try-error"))
    return(NULL)
  else
    return(x)
})

To see this in action, try a toy example. What try does is return the object, or in the event of an error a special object class containing details of the error:

x <- try(stop("Test error"))
inherits(x, "try-error")
x
# [1] "Error in try(stop(\"Test error\")) : Test error\n"
# attr(,"class")
# [1] "try-error"
# attr(,"condition")
# <simpleError in doTryCatch(return(expr), name, parentenv, handler): Test error>

Versus if you simply introduce an error without try the program will stop and x will be undefined:

rm(x)
x <- stop("Test error")
# Error: Test error
x
# Error: object 'x' not found

If the operation succeeds inside try(), it simply returns the correct object:

x <- try(1)
x
# [1] 1

Upvotes: 5

Related Questions