如果文件退出,则此行代码返回TRUE,如果目录不存在,则返回NA。
在这种情况下,目录不存在,因此该行返回
file.info("M:/T/2014/")[1,"isdir"]
[1] NA
现在我想抓住这个案子,所以我这样做:
if(file.info("M:/T/2014")[1,"isdir"] != TRUE){
print("there is no directory")
}
但是我收到了一个错误:
Error in if (file.info("M:/T/2014")[1, "isdir"] != TRUE) { :
missing value where TRUE/FALSE needed
我也尝试过:
if(as.character(file.info("M:/T/2014")[1,"isdir"]) == "NA"){
print("there is no directory")
}
你能告诉我该怎么做!在if语句中输入= TRUE或==“NA”吗?
谢谢。
答案 0 :(得分:1)
使用is.na
声明尝试这样的事情:
f <- file.info("M:/T/2014/")[1,"isdir"]
if(!is.na(f) && !f) print("there is no directory")
或许,你想要:
f <- file.info("M:/T/2014/")[1,"isdir"]
if(is.na(f) | !f) print("there is no directory")