I have a raster file of land cover which I have reduced to contain just tree cover cells. I have used clump
in the raster package to clump() together contiguous areas of forest. This gives all cells touching one another the same ID because they are part of the same patch.
I then want to figure out the PatchStat() for each clump, which I do by converting my clump raster to as.matrix. I tried to get the PatchStat() to do this to the raster but it would only work if it was in a matrix.
I now want to make a raster with the patch stat output, namely "perim.area.ratio". So each cell that corresponds to clump 1, will get the perim.area.ratio value that corresponds to clump 1. To do this I made a data.frame() from my clump raster that had: lon, lat, layer(clumpID), cellID
.
I tried to merge my clump raster data.frame with the PatchStat output using layer and patchID. However, an error occurs:
Error in fix.by(by.x, x) : 'by' must specify valid column(s).
Any ideas how I could do this another way, or why these columns aren't valid? Code below.
clump <- raster(file.choose())
library(SDMTools)
clumpval <- rasterToPoints(clump)
clumpcell <- cellFromXY(clump, clumpval[, c('x', 'y')] )
clumpdf <- data.frame(clumpval, clumpcell)
ps.data <- PatchStat(as.matrix(clump))
merged.data.all <- merge(clumpdf, ps.data1, by=c("layer", "patchID"))