根据列表/字符在目录中选择文件,出现pattern =

时间:2018-11-21 18:22:33

标签: r directory copy

我正尝试根据包含另一个文件夹中文件的列表,从“ rawimages”文件夹中选择文件。这个文件将有1000个,下面以一个为例。唯一的区别是RAW图像没有“ _hc”,但rawimages文件夹中还有其他文件。见下文:

ValidateImages <- list.files("C:/Users/JS22/Desktop/Validated")
#example of validated image name: Pia1.2016-10-07.2056+N2009_hc.tif
#Gives list as character value

ToselectfromRAW <- list.files("C:/Users/JS22/Desktop/Raw", pattern = ??ValidateImages??)
#example of raw image name: Pia1.2016-10-07.2056+N2009.tif  

file.copy(from = c(ToselectfromRAW), to = "C:/Users/JS22/Desktop/Training_Set", overwrite = TRUE)
#move the selected images to the desired location

我知道它与list.files函数的'pattern ='部分有关,但是我不确定是什么。如果有帮助,我可以使用以下方法将列表更改为列表:

ValidateImages<- as.list(list.files("C:/Users/JS22/Desktop/Validated", all.files = TRUE))

任何帮助都是伟大的!

(用于randomForest训练集fyi)

1 个答案:

答案 0 :(得分:0)

您可以获取目录中的文件列表,然后获取不包含grep的{​​{1}}:

_hc

这将为提供的测试文件输出以下内容:

directory <- "C:/Users/JS22/Desktop/Raw"

# remove "_hc.tif" from input filenames to match on
pattern <- gsub("_hc.tif", "", ValidateImages)
pattern <- paste(gsub("([.|()\\^{}+$*?]|\\[|\\])", "\\\\\\1", pattern), collapse="|")

grep(list.files(directory), pattern=pattern, value=TRUE)