0

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?

Update: when I turn list into a stack and the code below this is the error that I get: Error in if (m[ii] == 0) minp[ii] = 4 * n[ii] : missing value where TRUE/FALSE needed

library(raster) library(rgdal) library(SDMTools)
m <- "Z:/././"
allFiles <- list.files(m,pattern = "_ex0315.ovr", no.. = TRUE,recursive=TRUE)
allFiles<-stack(allFiles)

#PATCH STATS
install.packages("SDMTools")
library(SDMTools)

#Landscape patch analysis
#define a simple binary matrix

for (i in 1:length(allFiles)){
  A1 <- raster(allFiles[[i]])
  # plot(A1)
  A1[A1>0]<-1
  #do the connected component labelling
  ccl.mat = ConnCompLabel(A1)
  ps.data=list()
  #calculate the patch statistics
  ps.data[[i]] = PatchStat(ccl.mat)
  ps.data[[i]][1:nrow(ps.data[[i]]), ncol(ps.data[[i]])+1] <- allFiles[[i]]

}

Error in if (m[ii] == 0) minp[ii] = 4 * n[ii] : 
  missing value where TRUE/FALSE needed

closed as off-topic by PolyGeo Jan 4 '18 at 4:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

1

It did not need a stack, I just needed to take ps.data=list() out of the loop

m <- "E:/smt/results/"
allFiles <- list.files(m,pattern = "_ex0315.ovr", no.. = TRUE,recursive=TRUE)

#PATCH STATS via SDMTools


#Landscape patch analysis
#define a simple binary matrix
#A1 <- raster("Z:/././W4_2013-04-15_647/spreadgis/results/pt13_ex0315.ovr")

for (i in 1:length(allFiles)){
  A1 <- raster(allFiles[[i]])
  # plot(A1)
  A1[A1>0]<-1
  #do the connected component labelling
  ccl.mat = ConnCompLabel(A1)

  #calculate the patch statistics
  ps.data[[i]] = PatchStat(ccl.mat)
  ps.data[[i]][1:nrow(ps.data[[i]]), ncol(ps.data[[i]])+1] <- allFiles[[i]]

}

a<-do.call(rbind, ps.data)
write.csv(a, "testloop.csv")
  • 1
    I edited your answer to fix the code block and please never, and I repeat, NEVER force a library install in example code. I would also point out that since we do not have access to your data, this is not a reproducible example. – Jeffrey Evans Sep 9 '16 at 18:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.