[R] read.table : how to condition on error while opening file?

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Thu Feb 19 17:23:33 CET 2009


Hi Stephane,
see ?try
hth.


Stephane Bourgeois schrieb:
> Hi,
>
>  
>
> I'm using read.table in a loop, to read in multiple files. The problem
> is that when a file is missing there is an error message and the loop is
> broken; what I'd like to do is to test for the error and simply do
> "next" instead of breaking the loop. Anybody knows how to do that?
>
>  
>
> Example: 
>
>  
>
> filelist <- c("file1.txt", "file2.txt", "file3.txt")
>
>  
>
> for (i in 1:3) {
>
>   if (read.table(filelist[i]) == ERROR LOADING FILE) {
> # this is where I do not know how to write the condition
>
>     print(paste("error opening file ", filelist[i], sep=""))
>
>     next
>
>   } else {
>
>     tmp <- read.table(filelist[i])
>
>   }
>
> }
>
>  
>
>  
>
> Cheers,
>
>  
>
> Stephane
>
>
>
>
>   

-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/42803-8243
F ++49/40/42803-7790




More information about the R-help mailing list