I have a large RasterStack (114 geotiff images) that I have successfully geoprocessed (masked, etc.) in R, but I am having difficulty getting it to apply a simple conditional statement to each raster (all raster layers are the same extent, resolution, and are co-registered). I want to set all pixel values that are less than 95% of each raster's max value to NA. For example, if a layer's max was 85, then pixel values < 80.75 = NA. Here's my code:
#Get max value from each raster layer
r_max <- maxValue(rstack)
#Set all values < 95% of max to NA
rstack[rstack < (r_max * 0.95)] = NA
When I run this code on the entire raster stack, I get "Error in value[j, ] : incorrect number of dimensions." However, if I run it on a smaller set (14 or so), it works exactly as it should. Because I have successfully executed a number of similar operations (other conditional statements, masking,etc.) on the entire stack without error, I am not sure why it's throwing this error now. Any ideas?
I apologize if this has been discussed before, but I was unable to find such a post. If it does exist, please point me in that direction.
Thanks