Skip to main content
2 of 3
readability
Andre Silva
  • 10.3k
  • 12
  • 55
  • 109

Calculating Shannon's diversity using moving window in R

I have a raster where each pixel is a particular vegetation type. I'm interested in the diversity of vegetation types around each pixel.

I'd like to assign a Shannon's diversity index value (see this link for equation) to each pixel based on a moving window analysis of surrounding pixels.

I've come up with some code to count the number of unique values in the moving window using functions in the raster package (at least that is what I hope it is doing!), but I am having trouble with the rest of the needed code. Here is what I have so far:

fw<-focalWeight(veg_map,5000, "circle") # creates circular filter with a radius of 5000m
test_fcl<-focal(veg_map,w=fw, fun=function(x,
          ...){length(unique(na.omit(x))) }) #counts unique values in moving window #  (e.g. species richness)
KevinB
  • 115
  • 2
  • 7