简体   繁体   English

R file.info在if语句中不起作用

[英]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. 如果文件退出,则此行代码返回TRUE,如果目录不存在,则返回NA。

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? 您能建议在if语句中放入!= TRUE或==“ NA”的方法吗?

Thank you. 谢谢。

Try something like this using an is.na statement: 使用is.na语句尝试以下操作:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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