如何提取没有生成相应文件的标识符?
作为生成文件的输入的标识符:
PublicPathToken
检查生成的文件:
fileIden <- c('a-1','a-2','a-3','b-1','b-2','c-1','d-1','d-2','d-3','d-4')
如果所有过程都完全成功,则为预期文件
files <- list.files(".")
files
# [1] "a-2.csv" "a-3.csv" "b-1.csv" "c-1.csv" "d-3.csv"
# Generated here for reproducibility.
# files <- c("a-2.csv", "a-3.csv", "b-1.csv", "c-1.csv", "d-3.csv")
是否缺少任何预期的文件?
fileExp <- paste(fileIden, ".csv", sep = "")
# [1] "a-1.csv" "a-2.csv" "a-3.csv" "b-1.csv" "b-2.csv" "c-1.csv" "d-1.csv" "d-2.csv" "d-3.csv" "d-4.csv"
预期输出
fileMiss <- fileExp[!fileExp %in% files]
# [1] "a-1.csv" "b-2.csv" "d-1.csv" "d-2.csv" "d-4.csv"
我确信直接获得上述输出的简单过程无需创建文件:# "a-1" "b-2" "d-1" "d-2" "d-4"
,fileExp
。你能指导我吗?
答案 0 :(得分:1)
你可以这样做:
fileIden <- c('a-1','a-2','a-3','b-1','b-2','c-1','d-1','d-2','d-3','d-4')
file <- c("a-2.csv", "a-3.csv" ,"b-1.csv", "c-1.csv", "d-3.csv")
setdiff(fileIden, trimws(gsub("\\.csv","", file)))
另一种方法:
setdiff(fileIden, stringr::str_extract(file,"(.*)(?=\\.csv)"))
<强>逻辑强>:
setdiff
找到两个向量之间的差异,gsub
替换了&#34; .csv&#34;没有任何东西,我们将它们聚集在一起,找出这些载体之间的区别。
<强>输出:强>
#[1] "a-1" "b-2" "d-1" "d-2" "d-4"
答案 1 :(得分:0)
一种不太优雅的方法
result <- ifelse(fileIden %in% substr(file, 1, 3), "", fileIden)
result[result != ""]