使用R来堆叠各种文件夹的文件

使用R来堆叠各种文件夹的文件,r,stack,landsat,R,Stack,Landsat,我堆叠了陆地卫星图像的某些tif文件,如图所示: setwd("C:/Users/Landsat/L5__002072-09MAY-2006") may2006<-list.files(".",pattern="*B[123457]\\.tif$", ignore.case=TRUE) [1] "LT05_L1TP_002072_20060509_20161121_01_T1_B1.TIF" [2] "LT05_L1TP_002072_20060509_20161121_01_T1_

我堆叠了陆地卫星图像的某些tif文件,如图所示:

setwd("C:/Users/Landsat/L5__002072-09MAY-2006")
may2006<-list.files(".",pattern="*B[123457]\\.tif$", ignore.case=TRUE) 

[1] "LT05_L1TP_002072_20060509_20161121_01_T1_B1.TIF" 
[2] "LT05_L1TP_002072_20060509_20161121_01_T1_B2.TIF" 
[3] "LT05_L1TP_002072_20060509_20161121_01_T1_B3.TIF"
[4] "LT05_L1TP_002072_20060509_20161121_01_T1_B4.TIF"
[5] "LT05_L1TP_002072_20060509_20161121_01_T1_B5.TIF"
[7] "LT05_L1TP_002072_20060509_20161121_01_T1_B7.TIF"

landsat_stack <- stack(may2006)

我还没有检查这个,但希望它能起作用:

setwd("C:/Users/Landsat")    
a<-list.dirs(getwd(),recursive = FALSE )
flist <- list()
stackfile <- list()
for (i in 1:length(a)){  
    flist[[i]] <-  list.files(a[i], recursive = TRUE, full.names = TRUE, pattern = "tif$")  

    stackfile[[i]] <- stack(flist[[i]])
}
setwd(“C:/Users/Landsat”)

a谢谢@anup。我终于用这个代码解决了这个问题。它返回按文件夹堆叠的TIF图像列表

setwd("C:/Users/Landsat")    
a<-list.dirs(getwd(),recursive = FALSE )

landsat<- apply(a,function (dir){  
    img<-stack(list.files(path=dir,ignore.case= TRUE, 
    pattern="*B[123457]\\.tif$", full.names= TRUE))
}) 
setwd(“C:/Users/Landsat”)

ado您的意思是希望每个文件夹都是一个单独的堆栈,还是希望所有TIF都在一个堆栈中?
all_landsat是的,我想要的是为每个文件夹创建一个单独的堆栈。有可能吗?谢谢你,不过我正试着为每个文件夹分别做一个堆栈。总共有多少个文件夹?如果仅为3或4,则可以重复使用相同的读取文件和堆栈代码@我有很多文件夹(至少25个)@cotimass我已经更新了我的答案。这样行吗?我无法检查此项,因为我没有您的设置。谢谢@anup我在运行循环时出错:error in.local(.Object,…):此外:警告消息:in flist[I]
for (i in all_Landsat){
    if (grep(pattern="+B[123457]\\.tif$", ignore.case=FALSE)){
    stack(i) 
  }
}
setwd("C:/Users/Landsat")    
a<-list.dirs(getwd(),recursive = FALSE )
flist <- list()
stackfile <- list()
for (i in 1:length(a)){  
    flist[[i]] <-  list.files(a[i], recursive = TRUE, full.names = TRUE, pattern = "tif$")  

    stackfile[[i]] <- stack(flist[[i]])
}
setwd("C:/Users/Landsat")    
a<-list.dirs(getwd(),recursive = FALSE )

landsat<- apply(a,function (dir){  
    img<-stack(list.files(path=dir,ignore.case= TRUE, 
    pattern="*B[123457]\\.tif$", full.names= TRUE))
})