[R-sig-Geo] using the 'mask' function to set NAs in 'raster'

Robert J. Hijmans r.hijmans at gmail.com
Fri Sep 24 01:29:35 CEST 2010


Hi Jason,

count(mr, NA) should indeed be larger than or equal to count(m, NA);
as in the example below. No idea what is going on in your case. What
makes you say that the values are returned at zeros? Are you using an
old version? If not, could you send me the files?

Robert

> library(raster)
Loading required package: sp
raster version 1.5-9 (21-September-2010)
>
> r = raster(ncol=10, nrow=10)
> r[] = round(runif(ncell(r))*2)
> m = raster(ncol=10, nrow=10)
> m[] = round(runif(ncell(m)))
> r[r==0] <- NA
> m[m==0] <- NA
>
> mr <- mask(r, m)
>
> count(r, NA)
[1] 20
> count(m, NA)
[1] 51
> count(mr, NA)
[1] 59


On Thu, Sep 23, 2010 at 12:44 PM, Jason MacKenzie <jmackenzie at tnc.org> wrote:
> dear list members
>
> i want to create a new raster where all cells that are NA in my 'mask' are
> set to NA, but all other values stay the same as input rasters. instead of
> NAs, masked areas are getting returned as 0s (see counts below). am i
> missing a flag to set NAs?
>
> thanks in advance,
> j
>
>
>
> r    <- raster(raster.asc)
> m  <- raster(mask.asc)
> mr <- mask(r, m)
>
>> count (r, NA)
> [1] 20533362
>> count (m, NA)
> [1] 39269328
>> count (mr, NA)
> [1] 20533362
>
>
>
>
>
> --
>
> *Jason MacKenzie, PhD*
> *
> Climate Change Adaptation Fellow*
> The Nature Conservancy
> 201 Mission Street, 4th Floor
> San Francisco, CA 94105
> jmackenzie at tnc.org
>
> (415) 281 0472            [work]
> (415) 777 0244            [fax]
> (510) 717 1544            [cell]
> jasonbmackenzie        [skype]
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list