在R中循环遍历for循环的子目录

问题描述:

我有一个包含365个子目录的大目录,其中包含一年中每一天的图像。我创建了一个我想应用于这些子目录中的每个图像的函数。目前,这是我有:在R中循环遍历for循环的子目录

library(raster) 
library(zebu) 

#List all of the 365 sub-directories within my main directory 
days <- list.files(full.names = F , recursive =F, pattern='*X2000*') 

#Apply my function to each directory within "days" 
for(j in 1:length(days)){ 

    named <- paste0("full_",j) 

    in.list <- list.files(recursive = T, full.names = F) 

    stitched <- mosaicList(in.list) 

    writeRaster(stitched, path='D:/Scratch/DataConvert/Daymet_Data/Full/' , 
    filename=named, overwrite=TRUE) 
} 

这个循环的目的是给每个子目录中的功能“mosaicList”适用于图像。问题是,当for循环运行时,对象“in.list”包含与“days”相同的子目录,而不是在子目录内列出映像。因此,它尝试运行我在同一时间每个子目录功能,我得到的错误

Error: cannot allocate vector of size 14.2 Gb 

我是新于R,所以我不太肯定我已经错了。有没有人有任何解决这个问题的见解?

有在你的循环中list.files一个问题:对于list.files

in.list <- list.files(recursive = T, full.names = F) 

默认路径参数,这是当前目录“”。也许改为:

in.list <- list.files(path=days[j], recursive = T, full.names = T) 

将解决。

+0

当我尝试你的建议时,我的对象'in.list'结束了空。如果我将第一行更改为for((j in 1:length(days)){'并按照你的建议,我的'in.list'被创建并正确地列出目录中的图像,但j变成'1L' – ColinB

+0

糟糕,我没有改变'full.names = T'。我做了改变,现在它正在工作。感谢您的帮助! – ColinB