require: raster package
I am having trouble masking values of one raster using values in another. This is similar to this question:
but it doesn't have the moving window (focal function) as a component of the question. I have two rasters with the same dimension, extent, projection.
Raster "MAT2resampled" is a raster with a wide range of temperature indices
Raster "wintPCP" is a second raster with pixel values of 1, O, and NA
The only pixels I want from MAT2resampled are those with the value 1 in wintPCP.
I used this code:
PthreshWint2 <- mask(MAT2resampled, wintPCP)
When I plot PthreshWint2, it doesn't appear as though the MAT2resampled raster has been masked at all. I think I'm missing something simple.
I have posted the two rasters here:
https://drive.google.com/open?id=118kCu6QpoXSYmA17PpODRhHDJefHPkx1
https://drive.google.com/open?id=1N062TiHTHBqRYrclRW8O87WY-MX8taQg
Best Answer
You have a mask with 0, 1, and NA values and you want to keep only the 1s. Hence you need to convert 0 and NA to the same value.
Here's a bit of a trick. Diving
m
by itself results in NA wherem
is 0 or NA (since 0/0 returns NaN, which acts like NA inmask
) and 1 where its 1:not sure if that's any quicker than the other solutions.