【发布时间】:2019-02-11 01:45:49
【问题描述】:
我在一个文件夹中有许多 TIFF 文件(每个都属于一个图像日期),我想为尽可能多的唯一日期创建列表,然后用适当的文件填充这些列表。理想情况下,我希望有一个函数,用户只需更改日期列表,尽管我无法运行一个循环遍历我的日期列表的函数。相反,我尝试创建一个函数,并为每个唯一的日期运行它。
dates <- list('20180420', '20180522', '20180623', '20180725', '20180810')
# Make a list of all files in the data directory
allFilesDataDirectory <- list.files(path = dataDirectory, pattern = 'TIF$')
# allFilesDataDirectory is a list of 60 TIFF files with the same naming convention along the lines of LC08_L1TP_038037_20180810_20180815_01_T1_B9
allDateLists <- NULL
for (d in dates){
fileFolderDate <- NULL
dynamicDateNames <- paste0('fileListL8', d)
assign(dynamicDateNames, fileFolderDate)
allDateLists <- c(allDateLists, dynamicDateNames)
}
myFunction <- function(date, fileNameList){
# files first
for (i in allFilesDataDirectory){
# Create a list out of the file name by splitting up the name wherever there is a _ in the name
splitFileName <- unlist(strsplit(i, "[_]"))
if(grepl(splitFileName[4], date) & (grepl('B', splitFileName[8]))){
fileNameList <- c(fileNameList, i)
print(i)
}
else {
print('no')
}
}
}
myFunction(date = '20180623', fileNameList = 'fileListL820180623')
函数运行,但fileListL820180623为NULL。
当硬编码时,一切正常,我不确定其中的区别。我尝试使用 assign()(此处未显示),但它什么也没做。
for (i in allFilesDataDirectory){
# Create a list out of the file name by splitting up the name wherever there is a _ in the name
splitFileName <- unlist(strsplit(i, "[_]"))
if(grepl(splitFileName[4], '20180623') & (grepl('B', splitFileName[8]))){
fileListL820180623 <<- c(fileListL820180623, i)
}
else {
print('no')
}
}
【问题讨论】: