Jas*_*ine 3 statistics loops r function
您好,我想比较两个目录中的文件:
allfilenames <- list.files(path="../XYZ")
names1=allfilenames[grep("XYZ_*",allfilenames)]
numfiles <- length(names1)
allfilenames2 <- list.files(path="../ABC")
names2=allfilenames2[grep("ABC_*",allfilenames2)]
numfiles <- length(names2)
for(i in names1){
if(exists(i in names2){...}###?????
else...
}
Run Code Online (Sandbox Code Playgroud)
存在功能是不对的!还有另一个功能来查看另一个导演中的文件是否存在?
要测试一个字符串是否在另一个字符串中,请使用%in%,例如:
names <- c("A", "B", "D")
"A" %in% names
[1] TRUE
"C" %in% names
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
exists测试对象是否存在.在您的情况下,您只想测试语句是真还是假.
不exists,但是file.exists.
回应卡斯滕.
OP的问题:
是否还有另一个函数来查看[]是否存在来自一个导向器[y]的文件?
我的答案的扩展版本
file.exists(file.path(some_other_dir, a_file_from_one_directory))
Run Code Online (Sandbox Code Playgroud)
这充分回答了这个问题.