How to avoid a problematic file in loop in R? -


i looping throgh lots of files code stops when encounters problem 1 file:

files= paste("c:\\users\\data_ ", data1$column, "_",  data1$row, ".txt", sep="") for(i in 1:length(files)){ wf <- read.table(files[i], sep=' ', header=true) wf=subset(wf, !is.na(scan))  here, instance, `wf` be: [1] date1  date2  scan          <0 rows> (or 0-length row.names)  stuf............... } 

so code here stop , through error:

error in data.frame(date = resf$wddate, obs, mod = daf):  arguments imply differing number of rows: 0, 1 

i want loop ignore (problematic) file , not go further in loop goes read next file , on

use try( ..code.. ) or trycatch( ..code.. ) statement. here best link know explaining it, section on exception handling further down.

http://adv-r.had.co.nz/exceptions-debugging.html

here simple loop using try ignores file read errors

files <- c("f1","f2","f3") (i in 1:length(files)){   try(     {       print(files[i])       df <- read.csv(files[i])     },silent=t   ) } 

here 1 using trycatch ignores file read errors

 files <- c("f1","f2","f3")     (i in 1:length(files)){       trycatch(         {           print(files[i])           df <- read.csv(files[i])         },         error = function(c) "error",         warning = function(c) "warning",         message = function(c) "message"       )     } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -