遍历R中for循环中的子目录
Iterate through sub-directories in for loop in R
我有一个包含 365 个子目录的大目录,其中包含一年中每一天的图像。我创建了一个函数,我想将其应用于这些子目录中的每个图像。目前,这就是我所拥有的:
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 循环 运行s 时,对象 "in.list" 包含与 "days" 相同的子目录,而不是在子目录中列出图像。结果它试图同时为每个子目录 运行 我的函数,我得到错误
Error: cannot allocate vector of size 14.2 Gb
我是 R 的新手,所以我不太确定哪里出了问题。有没有人对解决这个问题有任何见解?
循环中的 list.files
有问题:
in.list <- list.files(recursive = T, full.names = F)
list.files的默认路径参数是“.”,即当前目录。也许更改为:
in.list <- list.files(path=days[j], recursive = T, full.names = T)
会修复。
我有一个包含 365 个子目录的大目录,其中包含一年中每一天的图像。我创建了一个函数,我想将其应用于这些子目录中的每个图像。目前,这就是我所拥有的:
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 循环 运行s 时,对象 "in.list" 包含与 "days" 相同的子目录,而不是在子目录中列出图像。结果它试图同时为每个子目录 运行 我的函数,我得到错误
Error: cannot allocate vector of size 14.2 Gb
我是 R 的新手,所以我不太确定哪里出了问题。有没有人对解决这个问题有任何见解?
循环中的 list.files
有问题:
in.list <- list.files(recursive = T, full.names = F)
list.files的默认路径参数是“.”,即当前目录。也许更改为:
in.list <- list.files(path=days[j], recursive = T, full.names = T)
会修复。