[R] Skipping missing files when importing data

Sundar Dorai-Raj sdorairaj at gmail.com
Mon Sep 21 06:32:09 CEST 2009


Try ?file.exists.

if (file.exists(fxxx)) {
  read.table(fxxx)
} else {
  cat("\"", fxxx, "\" is missing\n", sep = "")
}

HTH,

--sundar

On Sun, Sep 20, 2009 at 9:28 PM, jiangrm <jiangrm at gmail.com> wrote:
> Trying to import a bunch of data files named like f001, f002, ....f999. Some of the files may be
> missing and the missing files vary from time to time.
>
> Used for loop and read.table. When it reaches the missing file (say f100), it shows:
>
> Error in file(file, "r") : cannot open the connection
> In addition: Warning message:
> In file(file, "r") :
>  cannot open file 'f100': No such file or directory
>
> and the program stops.
>
> How can I skip the missing ones and keep the program running? Guess either checking the validity of
> filenames, or ignore the error message may work. Which functions should be used or any better ideas?
>
>
> -RJ
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list