[R-sig-Geo] Unexpected behavior of raster "mask" function

Alex Zvoleff azvoleff at conservation.org
Tue Sep 2 18:33:33 CEST 2014


Thanks Robert! Exactly what I needed.

Best,
Alex


On Sat, Aug 30, 2014 at 6:31 PM, Robert J. Hijmans <r.hijmans at gmail.com>
wrote:

> Alex,
> Thanks for the clear example. I had not considered that case.
> I have added an argument 'updateNA' to the mask function (version
> 2.2-43) such that you can do:
>
> masked_image <- mask(img, msk, updatevalue=2, updateNA=TRUE)
>
> That is, if updateNA is TRUE, NA cells outside the mask are also updated.
>
> Robert
>
>
> On Fri, Jul 18, 2014 at 11:18 AM, Alex Zvoleff
> <azvoleff at conservation.org> wrote:
> > I am using the mask function in the raster package (2.2-38) to mask out
> > areas within an image that are outside an area of interest (AOI). There
> are
> > NAs within this AOI that are meaningful - after masking the image I am
> > using freq to tabulate these NAs along with the other values in my AOI.
> For
> > this reason, I am using the "updatevalue" option in the mask function to
> > recode all areas outside of my AOI to a value (say 99) so that I can
> ignore
> > these areas in subsequent analysis, without having ignored areas share a
> > value (NA) with areas inside my AOI.
> >
> > However, the mask function does not operate as I expected - areas in the
> > image that should be masked but that are NA prior to masking are not set
> to
> > "updatevalue". Is there an way to tell the mask function to recode all
> > areas (regardless of their initial value) to updatevalue? The below
> example
> > shows my problem:
> >
> > library(raster)
> > # Setup an image with two NA values. Make one NA value inside
> > # the AOI, and make the other outside the AOI
> > img <- matrix(1, nrow=5, ncol=5)
> > img[3, 2] <- NA # Inside AOI
> > img[1, 2] <- NA # Outside AOI
> > img <- raster(img)
> > plot(img)
> >
> > # Setup a mask where only the 3x3 region in the center of img is retained
> > msk <- matrix(1, nrow=5, ncol=5)
> > msk[1,] <- NA
> > msk[5,] <- NA
> > msk[,1] <- NA
> > msk[,5] <- NA
> > msk <- raster(msk)
> > plot(msk)
> >
> > # Apply mask
> > masked_image <- mask(img, msk, updatevalue=2)
> >
> > # Notice that cell (1, 2) of masked_image is NA, instead of the
> updatevalue
> > (2).
> > # This cell was not affected by the mask because it was NA prior to
> calling
> > mask.
> > # Is there a way to tell the mask function to set all masked areas to
> > updatevalue,
> > # regardless of whether a cell is NA prior to masking?
> > plot(masked_image)
> >
> > Thanks,
> > Alex
> >
> > --
> > Alex Zvoleff
> > Postdoctoral Associate
> > Tropical Ecology Assessment and Monitoring (TEAM) Network
> > Conservation International
> > 2011 Crystal Dr. Suite 500, Arlington, Virginia 22202, USA
> > Tel: +1-703-341-2749, Fax: +1-703-979-0953, Skype: azvoleff
> > http://www.teamnetwork.org | http://www.conservation.org
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Alex Zvoleff
Postdoctoral Associate
Tropical Ecology Assessment and Monitoring (TEAM) Network
Conservation International
2011 Crystal Dr. Suite 500, Arlington, Virginia 22202, USA
Tel: +1-703-341-2749, Fax: +1-703-979-0953, Skype: azvoleff
http://www.teamnetwork.org | http://www.conservation.org

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list