I have a raster Brick which represents the distribution models of 7 palm species, named currentStack_mask
which looks like this.
As you can see I have 7 species and each of their rasters are represented with 0 and 1 values.
Basically what I want to do is (for each species) to extract all the cells that have a value of 1 and create another raster with those cells and of course to do it in R because I want to keep a track of what I am doing and also is because is faster and don't have to deal with all the intermediate files.
The equivalent function in Arcgis of what I want to do is Spatial Analyst Tools -> Extraction -> Extract by Attributes, which basically extract the cells of a raster based on a logical query, which in this case is that the cell value is 1.
I have tried with extract()
function of the Raster package but this function extract the values not the cells.
Can anybody help me?… I am sure there is a short way to do this.
Best Answer
I'm thinking in two different ways to achieve this. First, I'll recreate your data:
I expose here two approachs:
Which is faster?
Let's use the first one... You can save each layer to a list or create new objects based on layer name (or other name). Also, If you want to save it, jus simply add
writeRaster()
: