How to extract mean from a raster using extract function in R while considering raster cell values above 0?
I have multiple rasters with some negative cell values and no-data. I am able to ignore no data using na.rm = T
. But don't know how to ignore negative cell values or apply greater than 0 threshold.
Below is my code:
library(rgdal)
library(raster)
setwd("E:/R_data")
pointg <- readOGR("E:/R_data","euro")
rasterfiles <- list.files(".", pattern = "tif")
for(j in 1:length(rasterfiles)){
curRaster <- raster(paste("E:/R_data", rasterfiles[j], sep ="/"))
csvDatatoWrite <- data.frame(pointg$Name, extract(curRaster, pointg, fun=mean, na.rm=TRUE))
names(csvDatatoWrite)[2] <- substr(rasterfiles[j],1,7)
csvst <- csvDatatoWrite
csvtrans <- t(csvst)
write.table(csvtrans, file = "euro.csv", append = TRUE, sep = ",", row.names = TRUE, col.names = FALSE)
}