Below is code for calculating patch statistics on 34 rasters where each raster's name is written as the last column in the output. I'm trying to figure out what I'm missing to have the loop go through all rasters and write their results to one csv. Am missing an i somewhere or does it need to be a raster stack?
library(raster) library(rgdal) library(SDMTools)
m <- "Z:/././"
allFiles <- list.files(m,pattern = "_ex0315.ovr", no.. = TRUE,recursive=TRUE)
allFiles
#allFiles<-stack(allFiles)
for (i in 1:length(allFiles)){
b1 <- raster(allFiles[i])
b1[b1>0]<-1
#do the coected component labelling
ccl.mat = CoCompLabel(b1)
#calculate the patch statistics
ps.data = PatchStat(ccl.mat)
ps.data[,ncol(ps.data)+1] <- allFiles[i]
write.csv(ps.data, file=paste0(getwd(),names(allFiles[[i]]),".csv"),row.names=F)
}
