简体   繁体   中英

R file.info not working in if statement

This line of code returns TRUE if the file exits or NA if the directory does not exist.

In this case the directory does not exist so this line returns

file.info("M:/T/2014/")[1,"isdir"] 
[1] NA

Now I want to catch this case so I do:

 if(file.info("M:/T/2014")[1,"isdir"] != TRUE){
      print("there is no directory")
    }

But I get an error:

Error in if (file.info("M:/T/2014")[1, "isdir"] != TRUE) { : 
  missing value where TRUE/FALSE needed

I also tried:

if(as.character(file.info("M:/T/2014")[1,"isdir"]) == "NA"){
 print("there is no directory")
}

Can you advise what to do to put != TRUE or == "NA" in the if statement?

Thank you.

Try something like this using an is.na statement:

f <- file.info("M:/T/2014/")[1,"isdir"] 
if(!is.na(f) && !f) print("there is no directory")

Perhaps, though, you want:

f <- file.info("M:/T/2014/")[1,"isdir"] 
if(is.na(f) | !f) print("there is no directory")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM